Possible to catch missing `export default router.handler()`
larsqa opened this issue · 2 comments
larsqa commented
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 commented
I get this error too. I wish syntax like before
export const handler =createRouter().handler( `catch error here` )
...
import {hander} from '...'
handler.get(...)
silvaezequias commented
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
});
});
}