1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const common_1 = require("@nestjs/common");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const constants_1 = require("../constants");
|
7 | const helpers_1 = require("../helpers");
|
8 | class BaseExceptionFilter {
|
9 | constructor(applicationRef) {
|
10 | this.applicationRef = applicationRef;
|
11 | }
|
12 | catch(exception, host) {
|
13 | const applicationRef = this.applicationRef ||
|
14 | (this.httpAdapterHost && this.httpAdapterHost.httpAdapter);
|
15 | if (!(exception instanceof common_1.HttpException)) {
|
16 | return this.handleUnknownError(exception, host, applicationRef);
|
17 | }
|
18 | const res = exception.getResponse();
|
19 | const message = shared_utils_1.isObject(res)
|
20 | ? res
|
21 | : {
|
22 | statusCode: exception.getStatus(),
|
23 | message: res,
|
24 | };
|
25 | applicationRef.reply(host.getArgByIndex(1), message, exception.getStatus());
|
26 | }
|
27 | handleUnknownError(exception, host, applicationRef) {
|
28 | const body = {
|
29 | statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
|
30 | message: constants_1.MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
|
31 | };
|
32 | applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
|
33 | if (this.isExceptionObject(exception)) {
|
34 | return BaseExceptionFilter.logger.error(exception.message, exception.stack);
|
35 | }
|
36 | return BaseExceptionFilter.logger.error(exception);
|
37 | }
|
38 | isExceptionObject(err) {
|
39 | return shared_utils_1.isObject(err) && !!err.message;
|
40 | }
|
41 | }
|
42 | BaseExceptionFilter.logger = new common_1.Logger('ExceptionsHandler');
|
43 | tslib_1.__decorate([
|
44 | common_1.Optional(),
|
45 | common_1.Inject(),
|
46 | tslib_1.__metadata("design:type", helpers_1.HttpAdapterHost)
|
47 | ], BaseExceptionFilter.prototype, "httpAdapterHost", void 0);
|
48 | exports.BaseExceptionFilter = BaseExceptionFilter;
|