-
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
feat(example-validation-app): customize error #4969
Conversation
CI fails due to code formatting issues. Run |
3cced2a
to
96432e6
Compare
@dhmlau Please fix https://travis-ci.com/github/strongloop/loopback-next/jobs/303320121 as follows: cd examples/validation-app
rm -rf node_modules package-lock.json
cd ../..
npm i |
docs/site/Validation-REST-layer.md
Outdated
} | ||
} | ||
|
||
response.status(httpError.statusCode).end(JSON.stringify(httpError)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the best way how to present an error in the response. I believe this is going to drop the stack trace, which will make debugging difficult? At the same time, we don't want to print stack traces in production. All of this is handled by our default reject action. Can you please update this example to show how the custom error handler can fall back to the default implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhmlau , in our shopping example sequence, we catch an error like this : https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/sequence.ts#L72 inside the sequence, and pass them to default reject action .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emonddr, thanks for pointing to the code snippet!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if @bajtos is asking to handle the error like authentication sequence handles it.
docs/site/Validation-REST-layer.md
Outdated
} | ||
} | ||
|
||
response.status(httpError.statusCode).end(JSON.stringify(httpError)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhmlau , in our shopping example sequence, we catch an error like this : https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/sequence.ts#L72 inside the sequence, and pass them to default reject action .
86b2244
to
67eb967
Compare
docs/site/Validation-REST-layer.md
Outdated
errors is to | ||
[customize the sequence of actions](https://loopback.io/doc/en/lb4/Sequence.html#customizing-sequence-actions) | ||
using a | ||
[Provider](https://loopback.io/doc/en/lb4/apidocs.context.provider.html). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to link to https://loopback.io/doc/en/lb4/Creating-components.html#providers instead?
examples/validation-app/src/__tests__/acceptance/validate.acceptance.ts
Outdated
Show resolved
Hide resolved
examples/validation-app/src/interceptors/validate-phone-num.interceptor.ts
Show resolved
Hide resolved
examples/validation-app/src/interceptors/validate-phone-num.interceptor.ts
Outdated
Show resolved
Hide resolved
examples/validation-app/src/providers/my-validation-error-provider.ts
Outdated
Show resolved
Hide resolved
I was on a call with few IBMers interested in custom error handling and it occurred to me that the solution based on a custom Reject implementation may be too complex. Unless the developer want to share the custom error handling, then I think it's much easier to implement the custom path inside the sequence ( export class MySequence implements SequenceHandler {
// ...
async handle(context: RequestContext) {
try {
// ...
} catch (error) {
const httpError = <HttpErrors.HttpError>error;
if (request.url === '/coffee-shops') {
// if this is a validation error
if (httpError.statusCode === 422) {
const newError = {
message: 'My customized validation error message',
code: 'VALIDATION_FAILED',
resolution: 'Contact your admin for troubleshooting.',
};
// you can change the status code here too
response.status(422).send(JSON.stringify(newError));
// TODO: show how to log the error using RestBindings.SequenceActions.LOG_ERROR
// The error was handled
return;
}
}
// Otherwise fall back to the default error handler
this.reject(context, error);
}
}
} Just an idea to consider. |
For longer term, I think it would be best to implement support for a custom JSON serializer in strong-error-handler, see loopbackio/strong-error-handler#8. This will allow app developers to simply change |
Thanks @bajtos for the code snippet. I was trying to figure out how to fall back to the default implementation. :). |
f2f10e0
to
082113e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the new version with the custom error handling code inside the sequence as easier to understand and more elegant 👍
However, it seems that implementation details in my suggestion have confused you a bit, sorry for that! 🙈 Please see my comments below for more explanation, hopefully it will be all clear now 🤞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great 👏
Add code for customizing error to
validation-app
example.Will add docs after #4874 lands.
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈