title: How to catch error in nestjs when a query fails
body: <p>I am new to <code>nestjs</code> and was trying to convert my backend from <code>nodejs</code> to <code>nestjs</code>. Hope that makes sense? I am using `typeorm. But I am not sure as what could be the best of way of catching errors.</p>
  
  <p><code>entity.ts</code></p>
  
  <pre><code>import { Entity, Column, PrimaryGeneratedColumn, PrimaryColumn } from 'typeorm';
  
  @Entity()
  export class Course {
  
    @PrimaryColumn()
    course: string;
  
    @Column("varchar", { array: true })
    subject: string[];
  }
  </code></pre>
  
  <p><code>controller.ts</code></p>
  
  <pre><code>import { Controller, Get, Post, Body } from '@nestjs/common';
  import { CourseService } from './course.service';
  import { Course } from './course.entity';
  
  @Controller('course')
  export class CourseController {
      constructor(private courseService: CourseService) {}
  
      @Get()
      getCourses(): Promise&lt;Course[]&gt; {
          return this.courseService.findAll();
      }
  
      @Post()
      addCourse(@Body() courseDto: Course[]) {
          return this.courseService.create(courseDto);
      }
  }
  </code></pre>
  
  <p><code>service.ts</code></p>
  
  <pre><code>import { Injectable, Catch, ExceptionFilter, ArgumentsHost, ConflictException } from '@nestjs/common';
  import { InjectRepository } from '@nestjs/typeorm';
  import { Repository, QueryFailedError } from 'typeorm';
  import { Course } from './course.entity';
  
  @Injectable()
  export class CourseService {
      constructor(
          @InjectRepository(Course)
          private courseRepository: Repository&lt;Course&gt;,
      ) { }
  
      catch(exception: any, host: ArgumentsHost) {
          throw new Error("Error in course service." + exception.code);
      }
  
      findAll(): Promise&lt;Course[]&gt; {
          return this.courseRepository.find();
      }
  
      create(courseDto) {
          return this.courseRepository.insert(courseDto)
              .catch((err: any) =&gt; {
                  // throw new ConflictException();
  
                  switch (err.name) {
                      case 'QueryFailedError':
                          console.log("**++**" + JSON.stringify(err));
                          // throw new Error("Error" + err.message + "" + err.detail);
                          // throw new ConflictException();
                          throw JSON.stringify(err); //"Error creating a course" + err.message + "::: " + err.detail;
                      default:
                          throw err; 
  
                  }
              });
      }
  }
  </code></pre>
  
  <p>Now, all I am able to throw is <code>throw new ConflictException();</code>. I wanted to throw different errors based on the result, like -
  1. For duplicate record
  2. Missing mandatory fields
  3. etc</p>
  
  <p>But not sure how can we handle and customize the same and also make full use of nestjs.</p>
  
  <p>Like I see the below trace in console but <code>500, Internal server in postman</code> -</p>
  
  <pre><code>{"message":"duplicate key value violates unique constraint \"PK_d7fc152bc721b3f55a56ed3ad33\"","name":"QueryFailedError","length":293,"severity":"ERROR","code":"23505","detail":"Key (course)=(II) already exists.","schema":"public","table":"course","constraint":"PK_d7fc152bc721b3f55a56ed3ad33","file":"d:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\access\\nbtree\\nbtinsert.c","line":"535","routine":"_bt_check_unique","query":"INSERT INTO \"course\"(\"course\", \"subject\") VALUES ($1, $2)","parameters":["II",["A","B","C"]]}
  [Nest] 12152   - 04/10/2020, 1:18:40 PM   [ExceptionsHandler] {"message":"duplicate key value violates unique constraint \"PK_d7fc152bc721b3f55a56ed3ad33\"","name":"QueryFailedError","length":293,"severity":"ERROR","code":"23505","detail":"Key (course)=(II) already exists.","schema":"public","table":"course","constraint":"PK_d7fc152bc721b3f55a56ed3ad33","file":"d:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\access\\nbtree\\nbtinsert.c","line":"535","routine":"_bt_check_unique","query":"INSERT INTO \"course\"(\"course\", \"subject\") VALUES ($1, $2)","parameters":["II",["A","B","C"]]} +190924ms
  </code></pre>