Thursday, 25 May 2017

Express error handling in promises

I am having an issue with errors in express, and promisses.

I am trying to have the best way to handle errors, create a standard way to do it in my app.

However, I am in front of an issue :

validate(req.body)
.catch(e => next(new APIError(status = 400, errors = e)))
.then(validatedbody => searchdatabase(validatedbody))
.then(dbResult => validate(dbResult))
.catch(e => next(new APIError(status = 500, errors = e)))
// results from db should always be valid
.then(validatedDbResult => res.json(validatedDbResult))

If the body is wrong, we should stop and send the error, and the error will be sent using the error handling middleware, however dut to how promises work, it will also continue to the next step (searching in the DB).

What should I do ? And am I correctly handling errors in a first place ?

Thank you very much, Giltho



via Giltho

No comments:

Post a Comment