#Custom exception filters not throwing unhandled exceptions, which were otherwise handled by Nest

2 messages · Page 1 of 1 (latest)

clever glen
#

`export class DuplicateKeyExceptionFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
// Check if it's a duplicate key error
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request: Request = ctx.getRequest();

if (exception.code === 11000) {
  const value = exception.message.match(/(["'])(\\?.)*?\1/)[0];

  const message = `Duplicate field value: ${value}. Please use another value!`;

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

}`

I have this custom exception handler implemented globally. But it does not handle other exceptions, is it possible to pass those exceptions to the respective default handlers?

meager glacier
#

@clever glen Seeing you code, you only assigned Variable message to a specific message

Also, did you add all Exception filters to the provider array in app.mpdule.ts file?