Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - How to handle stream error events

I'm reading from a file stream returned from fs.createReadStream() and I'm piping it to a gzip stream created using zlib.createGzip() and then I'm piping the gzip stream to a HTTP response.

I'm not sure how to deal with the 'error' event on these streams. I just want to make sure that all the the streams get closed, that the error is logged, and that no resources are leaked (note that the file stream has autoClose set to true).

If an error happens on the fs read stream for example, how will that affect the gzip stream and then the response stream? Will the 'error' event be propagated automatically or will it just be unhandled and crash my app? Should I listen for the 'error' event on each of the streams or just on the last stream in the chain? What would happen if I listen to the 'error' on the fs stream - Will the gzip stream still detect that an error has occurred?

like image 926
Jon Avatar asked Nov 19 '13 01:11

Jon


People also ask

How do I fix a process out of memory exception in node JS?

This exception can be solved by increasing the default memory allocated to our program to the required memory by using the following command. Parameters: SPACE_REQD: Pass the increased memory space (in Megabytes).

What are the event of stream in node JS?

There are four fundamental stream types within Node.js: Writable : streams to which data can be written (for example, fs.createWriteStream() ). Readable : streams from which data can be read (for example, fs.createReadStream() ). Duplex : streams that are both Readable and Writable (for example, net.Socket ).


1 Answers

The errors from one stream to another is not propagated on piping, so you should be attaching the error listeners to both streams.

If the fs read stream has an error, if autoClose is true then it will be destroyed(it will clean up and close the file descriptor). But the gzip stream won't be closed so you have to close it manually.

If the gzip has an error, it will just emit it. It won't be closed, nor the readable stream will be.

Looking at other streams, like fs write stream, if an error happens on the writing, then it will close the writable stream, but the readable stream will stay open.

So my recommendation is that, set error handler on all your streams and don't rely on themselves closing on error, so call .close or .destroy on all errors.

To make sure you are listening on all error handlers, use domains.

like image 65
Farid Nouri Neshat Avatar answered Oct 13 '22 03:10

Farid Nouri Neshat