lib/shared/filters/db-exception-filter.ts
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.
Methods |
catch | |||||||||
catch(exception: QueryFailedError, host: ArgumentsHost)
|
|||||||||
Defined in lib/shared/filters/db-exception-filter.ts:27
|
|||||||||
Parameters :
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 });
}
}