Skip to content

Instantly share code, notes, and snippets.

@tomasAlabes
Last active August 11, 2020 16:25
Show Gist options
  • Save tomasAlabes/249229f97e22774b4ab9682105ce2ca6 to your computer and use it in GitHub Desktop.
Save tomasAlabes/249229f97e22774b4ab9682105ce2ca6 to your computer and use it in GitHub Desktop.
Custom http exception filter
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