forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws-exceptions-handler.js
More file actions
47 lines (47 loc) · 1.88 KB
/
Copy pathws-exceptions-handler.js
File metadata and controls
47 lines (47 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("@nestjs/core/constants");
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
const invalid_exception_filter_exception_1 = require("@nestjs/core/errors/exceptions/invalid-exception-filter.exception");
const ws_exception_1 = require("../exceptions/ws-exception");
class WsExceptionsHandler {
constructor() {
this.filters = [];
}
handle(exception, args) {
const client = args.switchToWs().getClient();
if (this.invokeCustomFilters(exception, args) || !client.emit)
return;
const status = 'error';
if (!(exception instanceof ws_exception_1.WsException)) {
const errorMessage = constants_1.messages.UNKNOWN_EXCEPTION_MESSAGE;
return client.emit('exception', { status, message: errorMessage });
}
const result = exception.getError();
const message = shared_utils_1.isObject(result)
? result
: {
status,
message: result,
};
client.emit('exception', message);
}
setCustomFilters(filters) {
if (!Array.isArray(filters)) {
throw new invalid_exception_filter_exception_1.InvalidExceptionFilterException();
}
this.filters = filters;
}
invokeCustomFilters(exception, args) {
if (shared_utils_1.isEmpty(this.filters))
return false;
const filter = this.filters.find(({ exceptionMetatypes, func }) => {
const hasMetatype = !exceptionMetatypes.length ||
!!exceptionMetatypes.find(ExceptionMetatype => exception instanceof ExceptionMetatype);
return hasMetatype;
});
filter && filter.func(exception, args);
return !!filter;
}
}
exports.WsExceptionsHandler = WsExceptionsHandler;