Node.js Error Handling

Node.js Error Handling – ERR_BUFFER_OUT_OF_BOUNDS

Node.js contains a plethora of possible errors it can throw during execution, so today we’ll continue our in-depth Node.js Error Handling series by looking at one of the many System Errors Node produces: the ERR_BUFFER_OUT_OF_BOUNDS. Node throws a System Error when an exception occurs within the program’s runtime environment, and such errors are typically an indication that there was an operational problem within the application. An ERR_BUFFER_OUT_OF_BOUNDS error indicates that an operation was made outside the bounds of a Buffer object.

Throughout this article we’ll explore the ERR_BUFFER_OUT_OF_BOUNDS system error by looking at where it sits in the overall Node.js Error Class Hierarchy. We’ll also explore the basic process of using Buffers in Node.js, which will illustrate how passing invalid bounds arguments (offsets or lengths, in particular) to some Buffer class methods can throw ERR_BUFFER_OUT_OF_BOUNDS errors. Let’s get into it!

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?

The Buffer class was introduced into early versions of Node.js to give developers a means of reading and manipulating streams of binary data. Since then, however, JavaScript ES6 (ECMAScript 2015) introduced the TypedArray object to handle binary data buffers. Therefore, modern Node.js versions have adapted the Buffer class to focus on implementing a more optimized Uint8Array API.

It’s also worth briefly noting that the Buffer class constructor has been deprecated, so the process of actually creating buffers should use the Buffer.from(), buffer.allocUnsafe(), and buffer.alloc() methods. For our example code we’ll be using the simplest of these, Buffer.from(value, encodingOrOffset, length), which retrieves a binary buffer representation of the first value argument passed to it. We can also specify additional arguments to alter the offset or length of the returned buffer.

To test this stuff out we have a simple bufferFromValue (value, encodingOrOffset, length) helper method:

As you can see, this method is merely a wrapper for the aforementioned Buffer.from(...) method, but it allows us to perform some simple, self-contained tests by passing different arguments. As such, we begin our tests by creating a simple data Uint8Array that is populated with the first five numeric values as keys (i.e. 0, 1, 2, 3, and 4):

We then pass the data.buffer value to bufferFromValue(...) to ensure everything works. Sure enough, the output we get is our Buffer object with the five expected values:

Next, let’s try passing an offset value of 3:

This also works just fine and outputs the same Buffer object as before, except we’ve offset the result by three values:

Cool! However, what happens if we pass an offset value that exceeds the bounds of the original array-like object?

As you can certainly guess, this throws a RangeError with a code property of ERR_BUFFER_OUT_OF_BOUNDS:

Since our buffer bounds only go up to 5, an offset of 6 or higher is invalid. Similarly, let’s try passing an in-bounds offset with an invalid length argument:

Once again, this throws an ERR_BUFFER_OUT_OF_BOUNDS RangeError, this time with the indication that length is outside the buffer bounds:

If we dig into the Node.js Buffer class source code we can quickly find exactly where these errors originate from. In this case, they are both thrown within the fromArrayBuffer(obj, byteOffset, length) internal function:

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!