Node.js Error Handling

Node.js Error Handling – RangeError

Continuing along through our in-depth Node.js Error Handling series, today we’ll be going over the RangeError. As with many other programming languages, including Ruby with the RangeError article we covered in that language, the Node.js RangeError is thrown to indicate that a passed function argument does not fall within the valid set or range of acceptable values. This might be because the value simply falls outside a given numeric range, or because there is a specific set of allowed values and the passed argument is not one of them.

Throughout this article we’ll examine the RangeError in more detail by first looking at where it resides in the larger Node.js Error Class Hierarchy. We’ll also look at some functional sample code illustrating how many of the built-in Node.js modules purposefully throw RangeErrors when invalid a arguments are passed, and how you might deal with them in your own code. Let’s get this party train rollin’!

The Technical Rundown

Most Node.js errors inherit from the Error base class, or extend from an inherited class therein. The full error hierarchy of this error is:

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

When Should You Use It?

As outlined in the introduction, a RangeError is intended to indicate when a passed function argument falls outside the bounds of allowed values, whether numeric or not within a set. To illustrate this in our code example we’re performing a simple client/server connection test using the net module, which provides an API for asynchronous networking functionality. Thus, our app.js begins by requiring the net module, along with our custom gw-logging module seen above, to help us simplify API calls for logging exceptions and other information to the console:

The connectToPort(port) function performs most of the logic for us, by attempting to create a connection via the net.createConnection() method. This client instance can then be used to attempt to connect to the passed port parameter, while we catch any unexpected Errors:

Our main test function, executeTests(), consists of first establishing a basic server via the net.createServer() method. We then open up a single port (24601) to listen for incoming client connections:

With the server created and listening at port 24601 we can start a series of test client connections at various ports via the connectToPort(port) function. First, let’s try the expected port of 24601:

Executing this code produces the following output:

Everything works as expected. Our server was created, we opened port 24601 on it to listen for incoming connections, then our client attempted and successfully connected to that same port, before the client disconnected and the socket was destroyed.

Now, let’s try connecting to a different port (31234), which the server isn’t actively listening on:

This produces an uncaught error, indicating that the server refused the connection at that port:

Finally, let’s connect to port number 1,000,000, which is well above the expected number of maximum ports for TCP/UDP connections (65535):

Executing this code doesn’t even attempt to connect to port 1000000 and give us a connection refusal message like before, but instead throws a RangeError instead, indicating that the port is well outside the bounds of allowed port range:

Airbrake’s robust error monitoring software provides real-time error monitoring and automatic error reporting for all your development projects. Airbrake’s state of the art web dashboard ensures you receive round-the-clock status updates on your application’s health and error rates. No matter what you’re working on, Airbrake easily integrates with all the most popular languages and frameworks. Plus, Airbrake makes it easy to customize error parameters, while giving you complete control of the active error filter system, so you only gather the errors that matter most.

Check out Airbrake’s error monitoring software today and see for yourself why so many of the world’s best engineering teams use Airbrake to revolutionize their exception handling practices!