title: How to use exception filter in nest.js
body: <p>According to document I developed <code>nest.js</code> exception filter.</p>
  <p><a href="https://docs.nestjs.com/exception-filters" rel="nofollow noreferrer">https://docs.nestjs.com/exception-filters</a></p>
  <p>I'd like to know how to use <code>catch</code> function in my <code>service</code></p>
  <p>I'd like to apply this <code>exception filter</code> to the <code>service</code></p>
  <p>As I am beginner. Are there any good way to apply <code>exceptionfilter</code> ?</p>
  <p>If someone has opinion, please let me know. Thanks</p>
  <pre><code>import {
    ArgumentsHost,
    Catch,
    HttpException,
    HttpStatus,
    Logger,
  } from '@nestjs/common';
  import { Request, Response } from 'express';
  
  @Catch()
  export class HttpExceptionFilter {
    catch(exception: HttpException, host: ArgumentsHost) {
      console.log(&quot;exception&quot;,exception.getResponse());
      const ctx = host.switchToHttp();
      const response = ctx.getResponse&lt;Response&gt;();
      const request = ctx.getRequest&lt;Request&gt;();
      const status =
        exception instanceof HttpException
          ? exception.getStatus()
          : HttpStatus.INTERNAL_SERVER_ERROR;
  
      const errorResponse = {
        code: status,
        timestamp: new Date().toLocaleDateString(),
        path: request.url,
        method: request.method,
        message: exception.message || null,
        error: exception.Error;
        
      };
  
      Logger.error(
        `${request.method} ${request.url}`,
        JSON.stringify(errorResponse),
        'ExceptionFilter',
      );
  
      response.status(status).json(errorResponse);
    }
  }
  
  </code></pre>
  <p>My service is folloiwng</p>
  <pre><code>import { Injectable, InternalServerErrorException } from '@nestjs/common';
  import { eventRepository } from 'src/repositories/event.repository';
  import { EventRequest } from './dto/event.dto';
  import { Event } from 'src/entities/event.entity';
  import { InsertResult, Like } from 'typeorm';
  
  @Injectable()
  export class EventService {
    constructor(private readonly eventRepository: eventRepository) {}
    async createEvent(eventParam: EventRequest): Promise&lt;Event&gt; {
      const event = new Event();
      Object.entries(eventParam).forEach(param =&gt; {
        event[param[0]] = param[1];
      });
      try {
        const insertResult = await this.eventRepository.insert(event);
        const id = insertResult.identifiers[0].id
        const Result = await this.eventRepository.findOne(id);
        return Result;
      } catch (error) {
        console.log(error);
        throw new InternalServerErrorException();
      }
    }
  }
  </code></pre>