Node.js (or other asynchronous frameworks): how to catch errors?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
9 views
Here's a question. Maybe my zaberemenila synchronous brain just does not see the solution that lies on the surface. But I don't understand how to handle errors in asynchronous approach, how to give the user 500-page. After all, how familiar synchronous request (schematically):
try { responce = (function request(req) { var user = auth.getUser(req); if (user.auth) { var data = db.getData(); if (data) { return new response(template.render(data)); } } return error404; })(req); } catch(e) { show500(e); }


All very reliable, the controller is obliged to return the response, any error not caught within the request are caught outside and give the user a nice page.

What with the asynchronous approach node.js?
try { (function request(req, res) { auth.getUser(req, function(user) { if ( ! user.auth) { return res.error404(); } db.getData(function(data) { if ( ! data) { return res.error404(); } Res. write(template.render(data)); }); }); })(req, res); } catch(e) { Res. show500(e); }


And now imagine that somewhere in the user.auth exception occurs. What will happen? And nothing, function(user) terminates, the answer is to treat no one, the connection to the browser will remain hanging until timeout. Even putting somewhere a global error handler, will not be able to return the user response, because the exception object is not the object access request and response.

Really, the only way to catch exceptions manually on each asynchronous call?
try { (function request(req, res) { auth.getUser(req, function(user) { try { if ( ! user.auth) { return res.error404(); } db.getData(function(data) { try { if ( ! data) { return res.error404(); } Res. write(template.render(data)); } catch(e) { Res. show500(e); } }); } catch(e) { Res. show500(e); } }); })(req, res); } catch(e) { Res. show500(e); }
by | 9 views

6 Answers

0 like 0 dislike
To node.js have an indirect relationship, but it always seemed to me that:
    \r
  • all means necessary to avoid exceptions thrown and return the error status
  • \r
  • call assynchronous function should be:
    asyncFunction(args, function(error, result){})
  • \r
And now, if we translate these points into your code:
auth.getUser(req, function(error,user) {
\r if (error != null){
handle(res,error);
\r return;
}
db.getData(function(error,data) {
\r if (error != null){
handle(res,error);
\r return;
}
Res. write(template.render(data));
});

Check exceptions here, I think, unnecessary. Weak spot only template.render(data), you just have to test this function throws exception. And even if so, it is better to check on or data valid before passing it to render. While he adhere to the rules, if the function for its normal work it is necessary more than 2/3 of if-s, then only try..catch. Unfortunately, in JavaScript, not so flexible to handle exceptions in .NET(sigh). I will be also glad if someone will offer a more constructive response.
by
0 like 0 dislike
Look at thechangelog.com/post/516202796/step-control-flow-the-node-js-way
\r
Just edinorosa error handling and exceptions.
by
0 like 0 dislike

Yuzaem domain http://habrahabr.ru/post/147233/

\r\r

In General, the specific processing of the node is callback(error, data) where colback flies error

by
0 like 0 dislike
Firebag — mega add-on for Mozilla Firefox that solves this problem:
in console you see all incoming and outgoing requests
— you see the contents of these queries
you can see how HTML and CSS
— you see error, including error messages server
— ...
\r
\rFirebag is installed from the Mozilla Firefox menu->edit->preferences->add-Ons - >manage add-ons->Get add-ons
by
0 like 0 dislike
I suggest to look in the direction co, koa.
by
0 like 0 dislike
generate everywhere event — type error_500, and stop work. and in the handler event already forming a response to user and print. then in any code is sufficient to generate the event and user needs page
by

Related questions

110,608 questions
257,186 answers
0 comments
28,000 users