Nest: HttpException getStatus() is not a function

Created on 2 Apr 2018  Â·  2Comments  Â·  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


I wrote my own exceptionFilter by implements @nestjs/common/ExceptionFilter:

@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, response) {
    const statusCode = exception.getStatus();
    response.status(statusCode).json({
      statusCode: statusCode,
      success: false,
      message: exception.getMessage()
      data: null
    });
  }
}

this is the HttpException file:

export declare class HttpException extends Error {
  private readonly response;
  private readonly status;
  readonly message: any;

  constructor(response: string | object, status: number);
  getResponse(): string | object;
  getStatus(): number;
}

as you can see, it provides two functions getStatus()and getResponse().

Expected behavior


I can get the expected return rather than it reported an error said exception.getStatus() is not a function.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: latest


For Tooling issues:
- Node version: v9.4  
- Platform:  Mac

Others:

question 🙌

Most helpful comment

Hi,
In your code I have noticed that your @Catch() is empty, that means it will try to catch all type of errors.
So you should in your implementation
In catch method
You need to check if the exception is instance of HttpException.

Another solution, is you could add what Type of errors your Filter should catch
In this case you need to replace @Catch()
by @Catch(HttpException).

Hope it helps.

All 2 comments

Hi,
In your code I have noticed that your @Catch() is empty, that means it will try to catch all type of errors.
So you should in your implementation
In catch method
You need to check if the exception is instance of HttpException.

Another solution, is you could add what Type of errors your Filter should catch
In this case you need to replace @Catch()
by @Catch(HttpException).

Hope it helps.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafal-rudnicki picture rafal-rudnicki  Â·  3Comments

2233322 picture 2233322  Â·  3Comments

artaommahe picture artaommahe  Â·  3Comments

cojack picture cojack  Â·  3Comments

thohoh picture thohoh  Â·  3Comments