File

src/all-exceptions.filter.ts

Implements

ExceptionFilter

Index

Properties
Methods

Properties

Private Readonly logger
Type : unknown
Default value : new Logger(AllExceptionsFilter.name)

Methods

catch
catch(exception: unknown, host: ArgumentsHost)
Parameters :
Name Type Optional
exception unknown No
host ArgumentsHost No
Returns : void
import {
    ArgumentsHost,
    Catch,
    ExceptionFilter,
    HttpException,
    HttpStatus,
    Logger,
} from "@nestjs/common";
import { Request, Response } from "express";

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
    private readonly logger = new Logger(AllExceptionsFilter.name);

    catch(exception: unknown, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse<Response>();
        const request = ctx.getRequest<Request>();

        const status =
            exception instanceof HttpException
                ? exception.getStatus()
                : HttpStatus.INTERNAL_SERVER_ERROR;

        const message =
            exception instanceof HttpException
                ? exception.getResponse()
                : (exception as any)?.message || exception;

        // Log the error with stack trace if available using NestJS Logger
        this.logger.error(
            `[${request.method}] ${request.url} ${status} - ${JSON.stringify(message)}`,
            (exception as any)?.stack,
        );

        response.status(status).json({
            statusCode: status,
            timestamp: new Date().toISOString(),
            path: request.url,
            message,
        });
    }
}

results matching ""

    No results matching ""