UNPKG

1.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.BaseWsExceptionFilter = void 0;
4const common_1 = require("@nestjs/common");
5const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6const constants_1 = require("@nestjs/core/constants");
7const ws_exception_1 = require("../errors/ws-exception");
8/**
9 * @publicApi
10 */
11class 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}
45exports.BaseWsExceptionFilter = BaseWsExceptionFilter;
46BaseWsExceptionFilter.logger = new common_1.Logger('WsExceptionsHandler');