Node.js Error Handling


Making our way through our in-depth Node.js Error Handling series, today we’ll be diving into the ERR_CONSOLE_WRITABLE_STREAM error, which is one of the many System Errors Node produces. Node throws System Errors when an exception occurs within the application’s runtime environment and such errors typically indicate that there was an operational problem within the program. An ERR_CONSOLE_WRITABLE_STREAM error indicates that an attempt was made to create a new Console API class without passing a valid stdout stream in which to place any generated output.

Throughout this article we’ll explore the ERR_CONSOLE_WRITABLE_STREAM error in a bit more detail, starting with where it resides in the overall Node.js Error Class Hierarchy. We’ll also look at some functional code samples illustrating how the Console class can be used to generate stream outputs, and how improper use of this class can potentially lead to ERR_CONSOLE_WRITABLE_STREAM errors in your own applications. Let’s get going!

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?

Let’s jump right into our sample code to understand what might cause an unexpected ERR_CONSOLE_WRITABLE_STREAM error. Our first helper function, getConsole(strout, stderr), is used to merely create a new Console class instance by passing along the provided stdout and stderr parameters:

With getConsole(...) setup we then have the addValueToStream(value, writeStream, errorStream) function, which does what the name suggests and generates a new Console instance, into which we insert the passed value parameter:

To make our sample code a bit more realistic we’re going to create some Book class instances, convert those to JSON strings, then add those strings to a new file via the fs.createWriteStream(...) method. The outputBookToStream(book, stream) function performs the stringification of the passed book value and passes the JSON string along to the addValueToStream(value, writeStream, errorStream) function:

Alright! We’re all set, so let’s test this out by creating our first Book instance, which we’ll then add to the newly-created write stream targeting the local books.json file:

Executing this test code produces the following output:

And, sure enough, the contents of books.json now contains the JSON we generated:

We’ll create a second, slightly different Book instance and combine both into an array before converting to JSON, since adding multiple objects one at a time to the books.json file doesn’t actually create valid JSON:

Our new books.json file now contains a collection of both Books:

As you can see, we’re explicitly creating and passing a writeStream value by calling fs.createWriteStream('books.json'). However, let’s do one more test and see what happens if we neglect to pass a valid Writable stream object:

As I’m sure you guessed might happen, failing to pass a valid stream up the stack (and, therefore, to the Console constructor) throws an ERR_CONSOLE_WRITABLE_STREAM TypeError:

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!