Class ExceptionWrapper<Exception>

A class for wrapping Next.js API route handlers to catch known application exceptions which should be sent to the client when thrown.

Automatically catches BaseValidationException exceptions from validateQuery, validateParams, and validateBody.

// exception-route-wrapper.ts

import { ExceptionWrapper } from 'next-api-utils';
import { CustomException } from './custom-exception';

function isException(maybeException: unknown): maybeException is CustomException {
return maybeException instanceof CustomException;
}

export const exceptionRouteWrapper = new ExceptionWrapper(isException);
// route.ts

import { NextResponse } from 'next/server';
import { exceptionRouteWrapper } from './exception-route-wrapper';
import { MyResponseSchema } from './my-response-schema';

export const GET = exceptionRouteWrapper.wrapRoute<MyResponseSchema>(() => {
return NextResponse.json({ foo: 'hello world' });
});

Type Parameters

Constructors

Methods

Constructors

Methods