Friday 29 May 2020

Express async error handling

Sorry if this has been posted before. I’ve just started learning Node and I am trying to write a basic mysql CRUD rest api.I basically got quite confused about how to handle errors in express. From what I read, one can either send all the errors to the error handling middleware by doing next(error) or monkey patch using express-async-error to allow direct throw in try catch block.My confusion is mainly around how to handle errors coming from db. Example I have a function which creates an entry in the database and I’m using try catch block to catch any errors result of the query. Now, I would connect the query function to my main controller function which would also be in a try catch block.Do I need to next(error) on both the db and controller functions? Or can I just return the error at the db level and handle the error at controller level?

Submitted May 29, 2020 at 04:05PM by jjbunny22

No comments:

Post a Comment