Body not set with custom message when using ctx.throw #1552
-
I am trying to set the message with a custom exception when calling ctx.throw but the results seem to be the default message for the status code being used. For example, let's say I have the following exception to custom handle 502 Bad Gateway: module.exports = function CustomException() {
this.message = 'Some custom exception message';
this.status = 502;
}; In a route I have code similar to the following that calls a service operation: try {
const response = await someService.doSomething();
} catch (err) {
ctx.throw(err.status, err.message);
} In someService.doSomething(), it throws the CustomException if an error occurs: try {
\\ execute some code
} catch (err) {
throw new CustomException();
} When I test this, the custom message 'Some custom exception message' is not returned in the response body. Instead I am getting 'Bad Gateway'. I was able to get this to work with a custom exception but it seems like the difference is for this latter case, the service is throwing that exception directly instead of as part of error handling of another error. In the case above, it seems like the original error may be taking precedence over the custom exception. Any suggestions on this are appreciated. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
you want a custom error handler for this. |
Beta Was this translation helpful? Give feedback.
you want a custom error handler for this.