Nestjs Monogdb Handel Catch Error

The solution for “Nestjs Monogdb Handel Catch Error” can be found here. The following code will assist you in solving the problem.

import { ArgumentsHost, Catch, ConflictException, ExceptionFilter } from ‘@nestjs/common’;
import { MongoError } from ‘mongodb’;

@Catch(MongoError)
export class MongoExceptionFilter implements ExceptionFilter {
catch(exception: MongoError, host: ArgumentsHost) {
switch (exception.code) {
case 11000:
// duplicate exception
// do whatever you want here, for instance send error to client
}
}
}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in