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 | class BaseWsExceptionFilter {
|
9 | catch(exception, host) {
|
10 | const client = host.switchToWs().getClient();
|
11 | this.handleError(client, exception);
|
12 | }
|
13 | handleError(client, exception) {
|
14 | if (!(exception instanceof ws_exception_1.WsException)) {
|
15 | return this.handleUnknownError(exception, client);
|
16 | }
|
17 | const status = 'error';
|
18 | const result = exception.getError();
|
19 | const message = (0, shared_utils_1.isObject)(result)
|
20 | ? result
|
21 | : {
|
22 | status,
|
23 | message: result,
|
24 | };
|
25 | client.emit('exception', message);
|
26 | }
|
27 | handleUnknownError(exception, client) {
|
28 | const status = 'error';
|
29 | client.emit('exception', {
|
30 | status,
|
31 | message: constants_1.MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
|
32 | });
|
33 | if (this.isExceptionObject(exception)) {
|
34 | return BaseWsExceptionFilter.logger.error(exception.message, exception.stack);
|
35 | }
|
36 | return BaseWsExceptionFilter.logger.error(exception);
|
37 | }
|
38 | isExceptionObject(err) {
|
39 | return (0, shared_utils_1.isObject)(err) && !!err.message;
|
40 | }
|
41 | }
|
42 | exports.BaseWsExceptionFilter = BaseWsExceptionFilter;
|
43 | BaseWsExceptionFilter.logger = new common_1.Logger('WsExceptionsHandler');
|