node.js - Handling exceptions in express -


i'm having trouble understanding how handle seems pretty basic aspect of express. if have code throws exception in async callback, there no way can catch exception because try/catch block no longer in scope time callback running. in these scenarios browser hang until give stating server unresponsive. bad user experience. rather able return 500 error client. default express error handler apparently not handle situation. here sample code:

var express = require("express");  var app = express(); app.use(app.router); //express error handler (never called) app.use(function(err, req, res, next) {     console.log(err);     res.send(500); });  app.get("/test", function(req, res, next) {     require("fs").readfile("/some/file", function(err, data) {         a.b(); //blow     }); });  app.listen(8888); 

in above code, line a.b() throws "referenceerror: not defined" exception. defined error handler never called. notice err object returned fs.readfile() null in case because file correctly read. bug code inside async handler.

i have read this post using node's uncaughtexpception even, the documentation says not use method. if did use it, how send 500 response user? express response object no longer around me use.

so how handle scenario?

ok, i'm going post entirely different answer since first answer has valuable information off topic in hindsight.

the short answer: correct thing happens: program should print stack trace , exit error.

the underlying thinking:

so think need think errors in different categories. first answer deals data-related errors well-written program can , should handle cleanly. describing crash. if read node.js documentation linked to, correct. useful thing program can @ point exit stack trace , allow process supervisor restart , attain understood state. once program has crashed, unrecoverable due extremely wide range of errors root cause of exception getting top of stack. in specific example, error going continue happening every time until source code bug fixed , application redeployed. if worried untested , buggy code going application, adding more untested , buggy error handling code isn't addressing right problem.

but in short, no, there's no way reference http request object caused exception afaik cannot change way perceived end user in browser, outside of handling @ intermediate reverse proxy layer configure crude timeout , send more friendly error page (which of course useless request isn't full html document).

the bible of error handling in node

error handling in node.js dave pacheco definitive work on topic in opinion. comprehensive, extensive, , thorough. recommend reading , re-reading periodically.


to address @asparagino's comments, if unhandled exception reproducible or happening high frequency, it's not edge case, it's bug. correct thing improve code not generate uncaught exceptions in face of situation. handle condition, converting programmer error operational error, , program can continue without restart , without uncaught exception.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -