next-server-middleware
Version:
A simple type-safe express-like middleware for Next.js api routes using app directory and next/server
13 lines • 615 B
JavaScript
// Importing NextResponse from next/server causes the app to crash
export const makeDefaultErrorFactory = ({ ResponseObj }) => (error, { errorStatus = 500 } = {}) => ResponseObj.json(error, { status: errorStatus });
export const withErrorHandling = ({ errorFactory, errorStatus = 500 }) => async (request, next) => {
try {
return await next();
}
catch (e) {
const endpointName = `${request.method} ${request.nextUrl.pathname}`;
console.error(`API error: ${endpointName}`, e);
return errorFactory(e, { errorStatus });
}
};
//# sourceMappingURL=withErrorHandling.js.map