How to: Next.js API Global Errors & Auth Middleware
––– views
•
2 mins
31 Dec 2023
import { ApiError } from "next/dist/server/api-utils"; import { NextResponse, NextRequest } from "next/server"; export const custom_middleware = (...handlers: Function[]) => async (req: NextRequest, res: NextResponse) => { try { for (const handler of handlers) { await handler(req, res); } } catch (error) { if (error instanceof ApiError) { return NextResponse.json( { message: error.message }, { status: error.statusCode } ); } else { /// Log server errors using winston or your preferred logger console.log(error); return NextResponse.json( { message: "Server died for some reason" }, { status: 500 } ); } } };
/// app/api/ping/route.ts import { custom_middleware } from "@/app/lib/server/middleware"; import { ApiError } from "next/dist/server/api-utils"; import { NextRequest, NextResponse } from "next/server"; const main_handler = (req: NextRequest, res: NextResponse) => { const isAuthenticated = false; if (isAuthenticated) { return NextResponse.json({ success: true }); } throw new ApiError(400, "Some error"); }; export const GET = custom_middleware(main_handler);
export const auth_middleware = async (req: NextRequest, res: NextResponse) => { /// Your auth logic const isAuthenticated = false; if (!isAuthenticated) { throw new ApiError(401, "Unauthorized"); } };
export const custom_middleware = (...handlers: Function[]) => async (req: NextRequest, res: NextResponse) => { try { /// /// Auth middleware await auth_middleware(req, res); for (const handler of handlers) { await handler(req, res); } } catch (error) { if (error instanceof ApiError) { return NextResponse.json( { message: error.message }, { status: error.statusCode } ); } else { /// Log server errors using winston or your preferred logger console.log(error); return NextResponse.json( { message: "Server died for some reason" }, { status: 500 } ); } } };