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

Can't modify message of ApiError in middleware #33

Open
wbobeirne opened this issue May 30, 2022 · 0 comments
Open

Can't modify message of ApiError in middleware #33

wbobeirne opened this issue May 30, 2022 · 0 comments

Comments

@wbobeirne
Copy link

I wanted to do some quick handling to set the err.message of ApiErrors to something in my body, so I whipped up a quick middleware that looks like this:

fetcher.use(async (url, init, next) => {
  try {
    return await next(url, init);
  } catch (err) {
    if (err instanceof ApiError && err.data?.message) {
      err.message = err.data.message;
    }
    throw err;
  }
});

However, I found that the error would still use the default message. I noticed that this is because of this bit: https://github.com/ajaishankar/openapi-typescript-fetch/blob/main/src/fetcher.ts#L207-L209. While I can make my own class that wraps errors to work around this, I would lose that nice getActualType typing. Would you be open to changing the ApiError constructor to respect an existing message if one has been set?

If you have a suggestion of a more "correct" way to do this, I'm all ears, but I use err.message as kind of a generic fallback in a lot of places in my code, and I don't want to have to make everything fetcher response body-aware to display better error messages.

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

No branches or pull requests

1 participant