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

How to set errorHandler globally in v1? #201

Open
larsqa opened this issue Jul 7, 2022 · 9 comments
Open

How to set errorHandler globally in v1? #201

larsqa opened this issue Jul 7, 2022 · 9 comments

Comments

@larsqa
Copy link

larsqa commented Jul 7, 2022

Before v1, i could create a base nc like

export const nc = () =>
    nextConnect({
          onError: (err, req, res) => {
            console.error(err.stack);
            res.status(500).end("Something broke!");
          },
          onNoMatch: (req, res) => {
            res.status(404).end("Page is not found");
          },
    });

And 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?

// Not what i want, but current behavior
const errorHandling = {
          onError: (err, req, res) => {
            console.error(err.stack);
            res.status(500).end("Something broke!");
          },
          onNoMatch: (req, res) => {
            res.status(404).end("Page is not found");
          },
}


...
// another file
import {router, errorHandling} from "./my-next-connect.js"
router.get(...)
export default router.handler(errorHandling)

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

@hoangvvo
Copy link
Owner

hoangvvo commented Jul 9, 2022

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;
};

@larsqa
Copy link
Author

larsqa commented Jul 11, 2022

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);

@hussamkhatib
Copy link

How about doing what's specified in

DO NOT reuse the same instance of router like the below pattern:
https://github.com/hoangvvo/next-connect#common-errors
Where you would clone it.

@hoangvvo I like the way it is done in previous version, Do I need to be worried about anything in previous version

@hoangvvo
Copy link
Owner

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);

Yup you are correct, I made a typo there.

@hoangvvo
Copy link
Owner

I can give this more thought, perhaps we can specify it as part of options (preferably nested inside a handler key for clarity) in createRouter function

@larsqa
Copy link
Author

larsqa commented Jul 12, 2022

Agree with @hussamkhatib, the new approach introduces more friction. I'd like to use it as it was before.

@IRediTOTO
Copy link

I like previous features too. I don't dare update Nc newest version :\

@raphaelbadia
Copy link

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;
};

Tried this, and it works perfectly. Thanks !

@nemanjahifolks
Copy link

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:

error - Error [TypeError]: resolver is not a function
    at /home/username/Desktop/folks/node_modules/next/dist/server/api-utils/node.js:377:16
    at /home/username/Desktop/folks/node_modules/next/dist/server/lib/trace/tracer.js:92:36
    at NoopContextManager.with (/home/username/Desktop/folks/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7057)
    at ContextAPI.with (/home/username/Desktop/folks/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:516)

How to fix this?

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

6 participants