0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.