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: Add custom error handler #14

Merged
merged 2 commits into from
Mar 5, 2023
Merged

feat: Add custom error handler #14

merged 2 commits into from
Mar 5, 2023

Conversation

trygve-lie
Copy link
Contributor

This adds a custom http error handler which take http-errors into account when constructing the http error page to send. This way its possible to control which http error to serve from where ever deeper down in the server code.

Podium do take http errors into account in the communication between layouts and podlets. A podlet can use http errors to signal to a layout what action to take if something fails in a podlet so we should make it easy to do this from the code the developer will write.

With this a user can then ex do stuff like this in a custom server.js file:

import httpError from 'http-errors';

export default async function server(app, { config, podlet }) {
    app.setContentState(async () => {
        try {
          const res = await fetch('https://website');
          return res.json();
        } catch (error) {
          // This controls the http error page being served
          throw new httpError.ImATeapot();
        }
    });
}

And the app will serve an error page (with correct http error status code) like so if the fetch() method to an upstream service errors:

{
    "statusCode": 418,
    "message": "I Am A Teapot"
}

Since this is based on passing on http-errors objects we might want to expose the internal http-errors module one way or another so the users don't have to import this them self (as the above example does). Maybe we should expose it along with the config, logger etc as such:

export default async function server(app, { config, podlet, errors }) {
    app.setContentState(async () => {
        try {
          const res = await fetch('https://website');
          return res.json();
        } catch (error) {
          // This controls the http error page being served
          throw new errors.ImATeapot();
        }
    });
}

Copy link
Member

@digitalsadhu digitalsadhu left a comment

Choose a reason for hiding this comment

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

Nice. I can add docs.

@digitalsadhu digitalsadhu merged commit 6b4981b into main Mar 5, 2023
@digitalsadhu digitalsadhu deleted the error-handler branch March 5, 2023 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants