title: Nest.js sending custom response to client from an Exception
body: <p>I'm facing a problem that i'd like to fix...</p>
  
  <p>I'm working on a procject using Nest.js as Backend framework for APIs, and Nuxt.js for Client...
  Everything is working fine, but when i'm trying to throw an error in a service, that is injected into the controller, i'm not able to send a custom response to the client. Those the scenarios that i've faced:</p>
  
  <p><strong>account.service.ts</strong></p>
  
  <pre><code> async createAccount(_account: AccountEntity){
      return await this.accountRepository.save(_account)
  }
  
  async _accountExists(_account: AccountEntity) {
      const itExists = await this.findOne(_account)
      if(itExists){
          throw new ConflictException(`Username already exists!`)
      }
  }
  </code></pre>
  
  <p>account.controller.ts</p>
  
  <pre><code>@Post()
  @UseFilters(new HttpExceptionFilter())
  async createAccount(@Body() userAccount: createAccountDto, @Res() res: Response) {
      try {
          await this.accountService._accountExists(userAccount).then(async () =&gt; {
              return await this.accountService.createAccount(userAccount)
          })
      } catch (e) {
          res.status(e.status).json(e.message)
      }
  }
  </code></pre>
  
  <p>This returns me this error in the client, if the user already exists but it doesn't send the json to the client.</p>
  
  <pre><code>POST http://localhost:3000/account 409 (Conflict)
  Request failed with status code 409
  </code></pre>
  
  <p>If i change it res.json(e), it sends me to the client the error with status 201 as you can see in the image, but the response is fine in all scenarios. 
  <a href="https://i.stack.imgur.com/zBEmm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zBEmm.png" alt="enter image description here"></a>
  So the question is... how i can get this response with correct status code?</p>
  
  <p>This is the Exception Filter:</p>
  
  <pre><code> import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
      import { Request, Response } from 'express';
  
      @Catch(HttpException)
      export class HttpExceptionFilter implements ExceptionFilter {
          catch(exception: HttpException, host: ArgumentsHost) {
              const ctx = host.switchToHttp();
              const response = ctx.getResponse&lt;Response&gt;();
              const request = ctx.getRequest&lt;Request&gt;();
              const status = exception.getStatus();
  
              response
                  .status(status)
                  .json({
                      statusCode: status,
                      name: exception.name,
                      message: exception.message.message,
                      timestamp: new Date().toISOString(),
                      path: request.url,
                  });
          }
      }
  </code></pre>