I am having a problem handling errors in an Express js application.
My problem is that I am passing the stream for a response, and I do not know what is the best method for handling errors that may occur in a readable stream.
I am using the errorHandler middleware configured right after the routing middleware:
... app.use(app.router); app.use(express.errorHandler()); ...
And this is my route:
exports.folders = function(req, res, next) { //throw new Error("TEST ERROR"); var path = decodeURIComponent(req.params.path), foldersStream = wd.listFolders(path); foldersStream.on("error",function(err){ console.log("STREAM ERROR") console.dir(next.name) return next(err); }); res.setHeader("content-type", "application/json"); foldersStream.pipe(res); };
If I throw a TEST ERROR into the function body, it is processed as expected by the errorHandler expression.
In any case, if an error event is errorHandler in the stream, the error event handler is called because I see the message in the console, but errorHandler never called.
If I do not handle the error event, the entire node process will terminate. If I process it, the server will send a 500 response to the client, but errorHandler not called, so I skip the stack trace in the log.
What am I doing wrong?
source share