@nestjs/core#BaseExceptionFilter TypeScript Examples

The following examples show how to use @nestjs/core#BaseExceptionFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: exception.filter.ts    From nestjs-enlighten with MIT License 6 votes vote down vote up
@Catch()
export class ExceptionFilter extends BaseExceptionFilter {
	private disableEnlighten: boolean
	private theme: string

	constructor(options: EnlightenConstructor = {
		disableEnlighten: false,
		theme: 'theme-light'
	}) {
		super()
		this.disableEnlighten = options.disableEnlighten
		this.theme = options.theme
	}

	catch(exception: any, host: ArgumentsHost) {
		if (host.getType() != 'http') {
			return;
		}
		const request = host.switchToHttp().getRequest()
		const response = host.switchToHttp().getResponse()

		if (this.disableEnlighten === true) {
			response.status(response.statusCode).json(exception.response)
			return
		}

		new ViewCompilerService(request, exception).getCompiledView(this.theme)
			.then(compiledView => {
				response.status(response.statusCode).send(compiledView)
			})
	}
}