1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.BaseWsExceptionFilter = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const constants_1 = require("@nestjs/core/constants");
|
7 | const ws_exception_1 = require("../errors/ws-exception");
|
8 |
|
9 |
|
10 |
|
11 | class BaseWsExceptionFilter {
|
12 | catch(exception, host) {
|
13 | const client = host.switchToWs().getClient();
|
14 | this.handleError(client, exception);
|
15 | }
|
16 | handleError(client, exception) {
|
17 | if (!(exception instanceof ws_exception_1.WsException)) {
|
18 | return this.handleUnknownError(exception, client);
|
19 | }
|
20 | const status = 'error';
|
21 | const result = exception.getError();
|
22 | const message = (0, shared_utils_1.isObject)(result)
|
23 | ? result
|
24 | : {
|
25 | status,
|
26 | message: result,
|
27 | };
|
28 | client.emit('exception', message);
|
29 | }
|
30 | handleUnknownError(exception, client) {
|
31 | const status = 'error';
|
32 | client.emit('exception', {
|
33 | status,
|
34 | message: constants_1.MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
|
35 | });
|
36 | if (this.isExceptionObject(exception)) {
|
37 | return BaseWsExceptionFilter.logger.error(exception.message, exception.stack);
|
38 | }
|
39 | return BaseWsExceptionFilter.logger.error(exception);
|
40 | }
|
41 | isExceptionObject(err) {
|
42 | return (0, shared_utils_1.isObject)(err) && !!err.message;
|
43 | }
|
44 | }
|
45 | exports.BaseWsExceptionFilter = BaseWsExceptionFilter;
|
46 | BaseWsExceptionFilter.logger = new common_1.Logger('WsExceptionsHandler');
|