Thank you for great article. Just quick note that this is not working for me now:
export const secureHandler = nc() .use(errorHandler) // we reuse a next-connect instance .use(apiLimiter) .use(bearerAuth) .use(acl);
The only way for me is to specify onError and onNotFound directly on nc instance:
onError
onNotFound
nc
export const secureHandler = nc({ onNoMatch: (req, res) => res.status(404).send({ ok: false, message: `API route not found: ${req.url}`, }), onError: (err, _req, res) => res.status(500).send({ ok: false, message: `Unexpected error.`, error: err.toString(), }), }) .use(apiLimiter) .use(bearerAuth) .use(acl);
Otherway the initial error callbacks are invoked.
Thanks! Indeed, my workaround here is to define a function that returns a new instance of a base handler:
export const baseHandler = () => nc({ // 404 error handler onNoMatch: (req, res) => res.status(404).send({ message: `API route not found: ${req.url}`, }), // 500 error handler onError: (err, req, res) => res.status(500).send({ message: `Unexpected error.`, error: err.toString(), }), }); export const secureHandler = baseHandler() .use(apiLimiter) .use(bearerAuth) .use(acl);
Article updated :).
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thank you for great article. Just quick note that this is not working for me now:
The only way for me is to specify
onError
andonNotFound
directly onnc
instance:Otherway the initial error callbacks are invoked.
Thanks! Indeed, my workaround here is to define a function that returns a new instance of a base handler:
Article updated :).