Skip to content
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

Merged
merged 1 commit into from
Apr 6, 2020
Merged

Conversation

dhmlau
Copy link
Member

@dhmlau dhmlau commented Mar 25, 2020

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 machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

👉 Check out how to submit a PR 👈

@raymondfeng
Copy link
Contributor

CI fails due to code formatting issues. Run npm run prettier:fix to fix it.

@dhmlau dhmlau force-pushed the customize-error branch 2 times, most recently from 3cced2a to 96432e6 Compare March 26, 2020 01:31
@raymondfeng
Copy link
Contributor

@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

@dhmlau dhmlau marked this pull request as ready for review March 27, 2020 20:05
@dhmlau dhmlau self-assigned this Mar 27, 2020
}
}

response.status(httpError.statusCode).end(JSON.stringify(httpError));
Copy link
Member

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?

Copy link
Contributor

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 .

Copy link
Member Author

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!

Copy link
Contributor

@emonddr emonddr left a 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 Show resolved Hide resolved
}
}

response.status(httpError.statusCode).end(JSON.stringify(httpError));
Copy link
Contributor

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 .

@dhmlau dhmlau force-pushed the customize-error branch from ae65fc4 to 76594bd Compare April 1, 2020 00:58
@dhmlau
Copy link
Member Author

dhmlau commented Apr 1, 2020

@emonddr @bajtos, for falling back to the default reject action, I used RejectProvider. Please see my latest commit 76594bd. Thanks.

@dhmlau dhmlau force-pushed the customize-error branch 2 times, most recently from 86b2244 to 67eb967 Compare April 1, 2020 01:21
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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/site/Validation-REST-layer.md Outdated Show resolved Hide resolved
@bajtos
Copy link
Member

bajtos commented Apr 2, 2020

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 (src/sequence.ts in projects scaffolded via lb4).

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.

@bajtos
Copy link
Member

bajtos commented Apr 2, 2020

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 RestBindings.ERROR_WRITER_OPTIONS to provide a custom function that will be called by LB to convert error objects into HTTP response bodies.

@dhmlau dhmlau force-pushed the customize-error branch from 5d1d13a to 312e496 Compare April 2, 2020 18:20
@dhmlau
Copy link
Member Author

dhmlau commented Apr 2, 2020

Thanks @bajtos for the code snippet. I was trying to figure out how to fall back to the default implementation. :).
I've updated accordingly to your review comments in 082113e, PTAL again, @nabdelgadir @bajtos. Thanks!

@dhmlau dhmlau force-pushed the customize-error branch 2 times, most recently from f2f10e0 to 082113e Compare April 2, 2020 21:48
Copy link
Member

@bajtos bajtos left a 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 🤞

docs/site/Validation-REST-layer.md Outdated Show resolved Hide resolved
docs/site/Validation-REST-layer.md Outdated Show resolved Hide resolved
docs/site/Validation-REST-layer.md Outdated Show resolved Hide resolved
docs/site/Validation-REST-layer.md Outdated Show resolved Hide resolved
@dhmlau
Copy link
Member Author

dhmlau commented Apr 3, 2020

@bajtos, please see my last commit a5ced91 on the changes. Thanks!

Copy link
Member

@bajtos bajtos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👏

docs/site/Validation-REST-layer.md Outdated Show resolved Hide resolved
@dhmlau dhmlau force-pushed the customize-error branch from fca1e71 to 9126261 Compare April 6, 2020 13:24
@dhmlau dhmlau merged commit d2a49a0 into master Apr 6, 2020
@dhmlau dhmlau deleted the customize-error branch April 6, 2020 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants