Last active
August 11, 2020 16:25
-
-
Save tomasAlabes/249229f97e22774b4ab9682105ce2ca6 to your computer and use it in GitHub Desktop.
Custom http exception filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import createError from 'http-errors'; | |
import { Catch, ArgumentsHost } from '@nestjs/common'; | |
import { BaseExceptionFilter } from '@nestjs/core'; | |
@Catch() | |
export class HttpExceptionFilter extends BaseExceptionFilter { | |
constructor(applicationRef) { | |
super(applicationRef) | |
} | |
catch(exception: any, host: ArgumentsHost) { | |
if(createError.isHttpError(exception)) { | |
this.applicationRef.reply(host.getArgByIndex(1), exception.message, exception.status); | |
} else { | |
super.catch(exception, host); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment