@opra/nestjs-http
Version:
Opra NestJS Http Module
40 lines (39 loc) • 1.39 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { Catch } from '@nestjs/common';
import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
/**
* OpraExceptionFilter
*
* NestJS exception filter that catches errors during OPRA HTTP requests
* and returns responses according to OPRA standards.
*/
let OpraExceptionFilter = class OpraExceptionFilter extends BaseExceptionFilter {
moduleRef;
constructor(moduleRef) {
super();
this.moduleRef = moduleRef;
}
/**
* Processes the caught exception.
* If the request has an OPRA context, it responds by converting the error to the OPRA error format.
* Otherwise, it uses the default NestJS exception handling mechanism.
*
* @param exception - The caught exception object.
* @param host - The arguments host.
*/
catch(exception, host) {
const ctx = host.switchToHttp().getRequest().opraContext;
if (ctx) {
const adapter = this.moduleRef.get(OpraHttpNestjsAdapter);
ctx.errors.push(exception);
return adapter.sendResponse(ctx);
}
super.catch(exception, host);
}
};
OpraExceptionFilter = __decorate([
Catch(),
__metadata("design:paramtypes", [ModuleRef])
], OpraExceptionFilter);
export { OpraExceptionFilter };