@nestjs/core
Version:
Nest - modern, fast, powerful node.js web framework (@core)
54 lines (53 loc) • 2.58 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
const constants_1 = require("../constants");
const helpers_1 = require("../helpers");
class BaseExceptionFilter {
constructor(applicationRef) {
this.applicationRef = applicationRef;
}
catch(exception, host) {
const applicationRef = this.applicationRef ||
(this.httpAdapterHost && this.httpAdapterHost.httpAdapter);
if (!(exception instanceof common_1.HttpException)) {
const body = {
statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
message: constants_1.MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
};
applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
if (this.isExceptionObject(exception)) {
return BaseExceptionFilter.logger.error(exception.message, exception.stack);
}
return BaseExceptionFilter.logger.error(exception);
}
const res = exception.getResponse();
const message = shared_utils_1.isObject(res)
? res
: {
statusCode: exception.getStatus(),
message: res,
};
applicationRef.reply(host.getArgByIndex(1), message, exception.getStatus());
}
isExceptionObject(err) {
return shared_utils_1.isObject(err) && !!err.message;
}
}
BaseExceptionFilter.logger = new common_1.Logger('ExceptionsHandler');
__decorate([
common_1.Optional(),
common_1.Inject(),
__metadata("design:type", helpers_1.HttpAdapterHost)
], BaseExceptionFilter.prototype, "httpAdapterHost", void 0);
exports.BaseExceptionFilter = BaseExceptionFilter;