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