File

src/core/filters/http-exception.filter.ts

Description

Basic filter for processing unhabdled HTTP exceptions

Implements

ExceptionFilter

Index

Methods

Methods

catch
catch(exception: HttpException, host: ArgumentsHost)

Exception handling method

Parameters :
Name Type Optional Description
exception HttpException No

Execution object currently being processed

host ArgumentsHost No

Arguments host utility object

Returns : void
import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from "@nestjs/common";

/**
 * Basic filter for processing unhabdled HTTP exceptions
 */
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
    /**
     * Exception handling method
     * @param exception Execution object currently being processed
     * @param host Arguments host utility object
     */
    catch(exception: HttpException, host: ArgumentsHost) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const status = exception.getStatus();

        response.status(status).json({
            status: status,
            message: exception.message,
        });
    }
}

results matching ""

    No results matching ""