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

Option to provide non-wrapped version of error (onError). #377

Closed
1 task done
beganovich opened this issue Apr 18, 2023 · 2 comments · Fixed by #479
Closed
1 task done

Option to provide non-wrapped version of error (onError). #377

beganovich opened this issue Apr 18, 2023 · 2 comments · Fixed by #479
Labels

Comments

@beganovich
Copy link

Describe the feature

Hi,

I've been implementing a custom error handler using onError and it works fine, except for one part. Since every error is wrapped using H3Error class, code like this isn't possible:

class CustomError extends Error {}

const app = createApp({
  onError(error, event) {
    if (error instanceof CustomError) {
      // ..
    }
  },
});

This would be super useful in case of custom validation error handling, for example, ZodError class. The key part missing is instanceof since each error is wrapped using H3 (https://github.com/unjs/h3/blob/main/src/error.ts#L55).

I was wondering, is there interest in providing an option to AppOptions for "vanilla" error classes?

Additional information

  • Would you be willing to help implement this feature?
@pi0
Copy link
Member

pi0 commented Aug 1, 2023

Hi and thanks for the suggestion. I think it is really valid cause to have the need of accessing the original error object. As a more generic fix, i think we can make original error accessible from wrapped object.

@pi0
Copy link
Member

pi0 commented Aug 1, 2023

With #479 in next release you can access original error simply using error.cause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants