[ ] 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.
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()
.
I can get the expected return rather than it reported an error said exception.getStatus() is not a function.
Nest version: latest
For Tooling issues:
- Node version: v9.4
- Platform: Mac
Others:
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.
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
methodYou need to check if the
exception
is instance ofHttpException
.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.