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

Possible to catch missing export default router.handler() #202

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

Possible to catch missing export default router.handler() #202

larsqa opened this issue Jul 7, 2022 · 2 comments

Comments

@larsqa
Copy link

larsqa commented Jul 7, 2022

With the new changes of v1, I found myself forgetting a couple of times to call .handler() when export default router.

This leads to Next.js throwing TypeError: resolver not a function. Is it somehow possible to catch this issue and provide a more user-friendly error message?

I'm aware of that I'm asking a lot here, since this Next.js scope related.

@IRediTOTO
Copy link

I get this error too. I wish syntax like before

export const handler =createRouter().handler( `catch error here` )
...
import {hander} from '...'
handler.get(...)


@silvaezequias
Copy link

If you are using the NextJS App/Router system, you can handle exceptions as follows:

import type { NextRequest } from "next/server";
import { createEdgeRouter, NextHandler } from "next-connect";

const router = createEdgeRouter<NextRequest, {}>();

router.get((request, context, next) => {
  if (true) {
    // force an exception to show custom error message
    throw new Error('This is my error message');
  }
  
  return Response.json({ message: 'hello world' }, {
    status: 200
  });
});

export async function GET(request: NextRequest, context: {}) {
  // trying to run the request but managing if there are any exceptions
  return router.run(request, context).catch((error) => {
    const errorMessage = error.message ?? 'Interanal Server Error Message';
   
    return Response.json({ message: errorMessage }, {
      status: 500
    });
  });
}

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

3 participants