0

I am using sails JS and mongo db for my web app. my code is working fine. But i am having a doubt.

my database statements are like this:

var a = await users.create({username:username,id:id}).fetch(); 

No try and catch. No error handling. it is working perfectly fine in my local host. If i Moved to production, will that create a problem??

Surendar
  • 153
  • 3
  • 11
  • but errors bubbles upwards, so you can also try/catch the parent function, or even add a global handler of all unhandled errors: `window.onunhandledrejection = event => { ... ` There is nothing that dictates that you must handle the error *here*. If you find yourself with many very similar error handlers, then I think that's a sign that you'd better handle the problem further up the chain. – ippi Aug 25 '20 at 05:15

2 Answers2

1

Any promise that has any chance of rejecting MUST have an error handler of some type. If you don't and you hit that error, your JS will essentially crash with an unhandled exception.

It's just bad programming not to handle any possible errors in some way. You should catch the error, log it and return some sort of error status from whatever http request probably initiated this call.

No error handling. it is working perfectly fine in my local host.

Sure, as long as there's NEVER an error with the database call, it works perfectly fine. What would you expect to happen if there was some sort of database error? Do you want your server crashing? Would you know what was causing it if your server just crashed? Would your users get any feedback that there's a problem?

If i Moved to production, will that create a problem??

It certainly could. If something happened with the data for this call or something happened to your database that led to an error, then your server may crash and whatever http request initiated this would never get a response.

It is just poor programming to not handle all possible errors in some way in your server. It's a common shortcut or lack of attention to detail, but it's always a mistake. As a senior developer involved in code reviews for less senior developers, this was a reason to fail the code review and require further attention to proper error handling.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • yeah.. I got your point. i am new to node js and also programming. As you said about promises, what best i can do in this case for db error? – Surendar Aug 25 '20 at 03:08
  • @Surendar: Most people just log the error. At minimum you can view your logs and see the error which would help you debug problems in production. In theory you can do other smart things like retry the query or reconnect to the db but almost nobody do them. The important thing is to catch the error otherwise node may do unexpected things like stop processing the event loop or exit the process. Currently node does not exit your process and merely print out an error message but node developers warn that they will exit the process in the future – slebetman Aug 25 '20 at 03:22
  • @Surendar - You would typically catch the error and then propagate it back to wherever it is your code would normally send a response to the incoming http request that initiated this call and then catch the error there and so something like: `console.log(err); res.sendStatus(500);` to log and send an error status. If this response is going to get displayed to the user in a browser, then you could also render an error page. – jfriend00 Aug 25 '20 at 03:23
  • @Surendar - Did this answer your question? – jfriend00 Aug 25 '20 at 04:54
0

No it will Not cause probleme but if you got error when this procedure is runing how would you know imagine you did that in many several places in your app and there is some ood issue happining how would you know where is coming from ?

so you Need to do that for more error handling!

Adam
  • 113
  • 1
  • 8