iLiA
iLiA

Reputation: 3153

how to get error status code from error object

i was doing my try catches and assinging all error status codes as 500 and i want to know if is it possible to get error status code for example inside catch block, like this:

instead of this

catch(err){
    return res.status(500).send({
        message: err.message
    })
}

this:

catch(err){
    return res.status(err.statusCode).send({
        message: err.message
    })
}

i checked docs but there is no word about this

Upvotes: 0

Views: 5122

Answers (2)

KernelPanic
KernelPanic

Reputation: 28

If this err inherits from Error class then its not possible. You can implement a wrapper in order to associate thrown errors with status 500. See https://nodejs.org/dist/latest-v10.x/docs/api/errors.html#errors_errors

Upvotes: 1

Sudhakar Ramasamy
Sudhakar Ramasamy

Reputation: 1759

You can't do 'err.statusCode'. Error object doesn't have any property named 'statusCode'.

Basically, you have to identify what http statusCode should be sent from server based on your outcome of the execution.

Upvotes: 0

Related Questions