title: NestJs - Catching MongoDB errors with a custom filter and @Catch(MongoError)
body: <p>I'm using NestJS to create a custom API with MongoDB. I've got the following setup:</p>
  
  <pre><code>  // users.controller.ts
  
    @Post('sign-up')
    @UseFilters(MongoExceptionFilter)
    async signUp(@Body() createUserDto: CreateUserDto): Promise&lt;any&gt; {
      return await this.userService.signUp(createUserDto).catch(error =&gt; {
        throw new BadRequestException(error);
      });
    }
  </code></pre>
  
  <pre><code>  // user.service.ts
  
    async signUp(createUserDto: CreateUserDto): Promise&lt;User&gt; {
    const createUser = new this.userModel(createUserDto);
      return await createUser.save();
    }
  </code></pre>
  
  <pre><code>  // mongo-exception.filter.ts
  
    import { ArgumentsHost,Catch, ConflictException, ExceptionFilter } from '@nestjs/common';
    import { MongoError } from 'mongodb';
  
    @Catch(MongoError)
    export class MongoExceptionFilter implements ExceptionFilter {
      catch(exception: MongoError, host: ArgumentsHost) {
        console.log('&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; exception: ', exception);
      }
    }
  </code></pre>
  
  <pre><code>  // package.json
    "dependencies": {
      "@nestjs/common": "^5.4.0",
      "@nestjs/core": "^5.4.0",
      "@nestjs/jwt": "^0.2.1",
      "@nestjs/mongoose": "^5.2.2",
      "@nestjs/passport": "^5.1.0",
      "@nestjs/typeorm": "^5.2.2",
      "fancy-log": "^1.3.3",
      "mongoose": "^5.4.7",
      "nestjs-config": "^1.3.0",
      "passport": "^0.4.0",
      "passport-http-bearer": "^1.0.1",
      "passport-jwt": "^4.0.0",
      "reflect-metadata": "^0.1.12",
      "rimraf": "^2.6.2",
      "rxjs": "^6.2.2",
      "typeorm": "^0.2.12",
      "typescript": "^3.0.1",
      "util": "^0.11.1"
    },
  </code></pre>
  
  <p>Now whenever I do a POST call to the /sign-up route, <code>save()</code> should be called in the <code>user.service.ts</code>. This all works. Next when I POST the /sign-up route another time it should trigger a MongoDB error since the user with the same email address is already (email address is unique and thus duplicate keys). I see the error is thrown when I just log the error in the <code>.catch(err =&gt; ...);</code>, but the problem is the custom <code>MongoExceptionFilter</code>. I won't trigger on the MongoError. When I leave the <code>@Catch()</code> blank it does trigger but can't process the exception.</p>
  
  <p>What am I doing wrong? Since I saw <a href="https://stackoverflow.com/questions/50864001/how-to-handle-mongoose-error-with-nestjs">this</a> post and used this as a foundation I can't seem to get it to work. Is it an update of Mongoose or NestJS why this isn't working anymore?</p>