-
Notifications
You must be signed in to change notification settings - Fork 86
Open
Description
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 ?
1valdis
Metadata
Metadata
Assignees
Labels
No labels