-
Notifications
You must be signed in to change notification settings - Fork 63
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
How to set errorHandler globally in v1? #201
Comments
That is an error on the doc, which I have removed. Unfortunately this is intended, providing options as part of nextConnect() is a bit misleading since calling .run() does not really respect these option, so I make it explicit that only .handler() will repsect it, thus the fact that it is only there. Perhaps you can try something like below: export const nc = () => {
const router = createRouter();
const handler = router.handler.bind(router);
// rewrite router.handler so that when you call it, it is called with the options
router.handler = () => handler({ ...yourOptions });
return router;
}; |
Isn't const router = createRouter();
const handler = router.handler.bind(handler); wrong? Isn't it supposed to be const router = createRouter();
const handler = router.handler.bind(router); |
How about doing what's specified in
@hoangvvo I like the way it is done in previous version, Do I need to be worried about anything in previous version |
Yup you are correct, I made a typo there. |
I can give this more thought, perhaps we can specify it as part of options (preferably nested inside a |
Agree with @hussamkhatib, the new approach introduces more friction. I'd like to use it as it was before. |
I like previous features too. I don't dare update Nc newest version :\ |
Tried this, and it works perfectly. Thanks ! |
This is not an elegant solution because now in every api route file I need to export this: export default handler.handler(); instead of nicer: export default handler; or I get this error:
How to fix this? |
Before v1, i could create a base
nc
likeAnd then simply use
nc().get(...)
.However, now in v1, router expects the errorHandling to be passed into the
router.handler()
method. Meaning, to achieve the same thing I would have to create an errorHandler object and pass it into the handler. This leads to two imports. Is it possible to set this on a base level?Edit
Following your documentations, it should be possible to add this options object inside the
createRouter()
(https://github.com/hoangvvo/next-connect#router--createrouteroptions).But when looking through the source code, this does not seem to be implemented: https://github.com/hoangvvo/next-connect/blob/main/src/node.ts
The text was updated successfully, but these errors were encountered: