📜  Nestjs Monogdb Handel Catch 错误 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:22.951000             🧑  作者: Mango

代码示例1
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
    }
  }
}