-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to customize the JSON content of HTTP error responses #1867
Comments
@hbakhtiyor Do you expect the error thrown by the controller code to override our built-in validation? |
@raymondfeng I think he's asking after applying his own validation if (!Number.isInteger(limit) || limit < 1) {
// how to custom the response here
} how to custom the response like https://github.com/strongloop/loopback-next/pull/1753/files#diff-3497d60dccb2aa1d1fdf2955a7a080e9R239 |
I don't think the controller method is invoked at all as the validation of |
@jannyHou yeah, @raymondfeng but the response matters, need in json and the structure |
@hbakhtiyor Building HTTP error responses is a tricky business. It's easy to get it wrong and open your application to attacks. In LoopBack (both 3.x and 4.x), we use our strong-error-handler middleware to take care of this. See Handling Errors in our docs. Here are the important security constraints to keep in mind:
Now that I have warned you, LoopBack 4 makes it very easy to format the error messages your way. Just provide a custom implementation of the Sequence action export class CustomRejectProvider implements Provider<Reject> {
// ...
action({request, response}: HandlerContext, error: Error) {
// handle the error and send back the error response
// "response" is an Express Response object
}
} Caveat: some errors thrown by LB4 have only |
@bajtos I have attempted making a custom error provider which takes two arguments: an array of errors and statusCode based on your comment. Here is the code I used: export class CustomRejectProvider implements Provider<Reject> {
constructor(
@inject('customErrors') public errors: Array<Object>,
@inject('customErrorCode') public stathsCode: number,
) { }
value() {
// Use the lambda syntax to preserve the "this" scope for future calls!
console.log("test1");
return (response: HandlerContext, result: Error) => {
this.action(response, result);
};
}
action({ request, response }: HandlerContext, error: Error) {
// handle the error and send back the error response
// "response" is an Express Response object
console.log("test2");
if (error) {
console.log("test3");
error.message = 'Message: my error';
error.name = 'Name: some error';
const headers = (request.headers as any) || {};
const header = headers.accept || 'application/json';
response.setHeader('Content-Type', header);
response.sendStatus(401);
response.end(error);
} else {
console.log("test4");
response.end(response);
}
} Response: {
"errors": [{
"msg": "first element"
}, {
"msg": "second element"
}],
"statusCode": 401
}
The response however contains only the two injected variables (the error array and the statusCode) exactly as provided to the class without any processing. All the console log statements in my code have no effect at all. Any tips? I was also unable to modify the statusCode of the response. It's always 200 OK. |
@shadyanwar response.sendStatus(401);
response.end(error); with response.status(401).send(error); and try, it worked for me. |
Thanks @shadyanwar @pktippa. I've created PR based on the above suggestions. See https://github.com/strongloop/loopback-next/pull/4969/files. @hbakhtiyor, we believe we've addressed your question. Closing as done. Please feel free to open a new issue if you find other problems. Thanks. |
for example
invalid fields will result in a
422 unprocessable entity
response with json body.The text was updated successfully, but these errors were encountered: