title: How can I handle TypeORM error in NestJS?
body: <p>I'd like to create a custom exception filter that handles different kinds of TypeORM errors. I've looked up the <a href="https://github.com/typeorm/typeorm/tree/master/src/error" rel="noreferrer">TypeORM error classes</a>, and it seems like there's no such thing in TypeORM like <a href="https://mongodb.github.io/node-mongodb-native/core/api/MongoError.html" rel="noreferrer">MongoError</a>.</p>
  
  <p>I wanted to make something similar to <a href="https://stackoverflow.com/a/51617106/3252438">1FpGLLjZSZMx6k's answer</a>, and here's what I've done so far.</p>
  
  <pre class="lang-js prettyprint-override"><code>import { QueryFailedError } from 'typeorm';
  
  @Catch(QueryFailedError)
  export class QueryFailedExceptionFilter implements ExceptionFilter {
    catch(exception: QueryFailedError, host: ArgumentsHost) {
      const context = host.switchToHttp();
      const response = context.getResponse&lt;Response&gt;();
      const request = context.getRequest&lt;Request&gt;();
      const { url } = request;
      const { name } = exception;
      const errorResponse = {
        path: url,
        timestamp: new Date().toISOString(),
        message: name,
      };
  
      response.status(HttpStatus.BAD_REQUEST).json(errorResponse);
    }
  }
  
  </code></pre>
  
  <p>If I need to catch another error for instance, <code>EntityNotFoundError</code>, I have to write the same code which is a very cumbersome task to do it.</p>
  
  <p>It would be nice if I could handle errors by a single filter like below. Any ideas?</p>
  
  <pre class="lang-js prettyprint-override"><code>@Catch(TypeORMError)
  export class EntityNotFoundExceptionFilter implements ExceptionFilter {
    catch(exception: MongoError, host: ArgumentsHost) {
      switch (exception.code) {
        case some error code:
          // handle error
      }
    }
  }
  </code></pre>