Surendar
Surendar

Reputation: 153

Await in async function without try and catch in sails mongo db

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??

Upvotes: 0

Views: 220

Answers (2)

jfriend00
jfriend00

Reputation: 707248

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.

Upvotes: 1

Adam
Adam

Reputation: 133

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!

Upvotes: 0

Related Questions