File

lib/shared/filters/db-exception-filter.ts

Description

Exception filter to handle database errors and transform them into HTTP responses. This filter catches TypeORM errors and returns a 502 Bad Gateway response with the error message.

Implements

DbExceptionFilter

Index

Methods

Methods

catch
catch(exception: QueryFailedError, host: ArgumentsHost)
Parameters :
Name Type Optional
exception QueryFailedError No
host ArgumentsHost No
Returns : void
import { ArgumentsHost, Catch } from "@nestjs/common";
import { QueryFailedError } from "typeorm";
import { TypeORMError } from "typeorm/error/TypeORMError";

/**
 * Exception filter to handle database errors and transform them into HTTP responses.
 * This filter catches TypeORM errors and returns a 502 Bad Gateway response with the error message.
 */
@Catch(TypeORMError)
export class DbExceptionFilter implements DbExceptionFilter {
  catch(exception: QueryFailedError, host: ArgumentsHost) {
    const response = host.switchToHttp().getResponse();
    response.status(502).json({ message: exception.message });
  }
}

results matching ""

    No results matching ""