Hi!
I've read the http://expressjs.com/guide.html#error-handling and gone
through this:
http://stackoverflow.com/questions/7151487/error-handling-principles-for-nodejs-express-apps
I've also written couple of pages of tests and I still cannot figure
out how I should return errors to the users (REST json API). So, can
somebody explain it to me.
I'm trying to handle errors from middleware and from routes. I want to
send different responseCodes (like invalid query, data not found,
internal server error) as responses to the API.
First, it took me a while to figure out that
"app.use(express.errorHandler({ showStack: true, dumpExceptions: true
})); " needs to have the "app.use(app.router); " called before it.
Yes, I can now read it from the page, but it was not that clear to me.
So, I have probably missed something else too.
So, some questions now:
- The doc says that there is a "app.error(function(err, req, res,
next){" that get's called with "next(err);" However, the stackoverflow
thread says that it's deprecated and now its "app.use(function(err,
req, res, next) {". Which one is correct?
- The doc says that app.error is called with next(err) - but what is
in err? There is complicated example, 'function NotFound(msg){', ok,
it's not that complicated, but I didn't think I need to do that much
code for the error. The code at
https://github.com/visionmedia/express/blob/master/examples/error-pages/app.js
uses more simple new Error('message'). What should the err contain?
- According to doc, app.error is called with next(err) or by throw
err. The writer in stackoverflow says throwing is bad practice - why?
What is the difference (except that code doesn't continue after
throw)?
- Am I right in assuming that I cannot have express.errorHandler and
app.error at the same time. express.errorHandler sounds like a good
way to do errors (if it can for instance server json also), however it
seems to me that it always returns 500 (var err = new
Error('Failure');err.status = 401;next(err); -> still shows 500 as
statusCode.
So, I guess I should use "app.use(function(err, req, res, next) {" to
get some other errors than 500. But I'm not really sure if I should
"throw { status: 401, message: "failure" }" or call "next(err)" with
"err = new Error('failure'); err.status=401".
How do you guys handle these?
--
HG.
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en