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

[fature idea] Hook: withCustomError #569

Open
ghsamm opened this issue Jan 28, 2020 · 4 comments · May be fixed by #764
Open

[fature idea] Hook: withCustomError #569

ghsamm opened this issue Jan 28, 2020 · 4 comments · May be fixed by #764

Comments

@ghsamm
Copy link

ghsamm commented Jan 28, 2020

Hello guys,

I'm submitting the basic idea here and once you approve it, I'll add the PR with types and tests et al.

The hook is called withCustomError and it can be used to change errors thrown by other hooks.

The scenario goes like this: I want to use the disable hook but return a 404 NotFound error instead of the default MethodNotAllowed error.

Basically, instead of writing the following e.g:

disallow("external") // throws MethodNotAllowed

We would wrap it in a withCustomError utility hook, like so:

withCustomError(disallow("extenral"), () => new NotFound()) // throws NotFound

The basic code goes like this:

const withCustomError = (hook, errorCreator) => (context: any) => {
  try {
   // if the hook does not produce an error, no need to throw
    const res = hook(context);
    return res;
  } catch (err) {
    throw errorCreator(context);
  }
};

We can also use function composition to create a new disallow404 like so:

const disallow404 = (...providers) => withCustomError(disallow(...providers), () => new NotFound());

What do you think ?

@ghsamm ghsamm changed the title Hook idea: withCustomError [fature idea] Hook: withCustomError Jan 28, 2020
@1valdis
Copy link

1valdis commented Feb 13, 2020

That would definitely be useful if an error needs to be, for example, localized.

@michaelwiles
Copy link

+1

@michaelwiles
Copy link

For the record, in case anyone else copies this, an await is required before the call to the internal hook.

const withCustomError = (hook, errorCreator) => (context: any) => {
  try {
   // if the hook does not produce an error, no need to throw
    const res = await hook(context);
    return res;
  } catch (err) {
    throw errorCreator(context);
  }
};

@fratzinger
Copy link
Collaborator

Thanks for the suggestion and sorry for the late response. I started working on a massive rewrite of the package (#764). It's not released yet and will take a few weeks.

This should be working then. I took a more drastical approach. I introduced a new hook called throwIfIsProvider which basically works the same as disallow but has a more clear naming and has an error option to change the error, see https://github.com/feathersjs-ecosystem/feathers-hooks-common/blob/modernize/src/hooks/throw-if/throw-if-is-provider.ts

@fratzinger fratzinger linked a pull request Feb 18, 2025 that will close this issue
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 a pull request may close this issue.

4 participants