forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc-exceptions-handler.js
More file actions
43 lines (43 loc) · 1.8 KB
/
Copy pathrpc-exceptions-handler.js
File metadata and controls
43 lines (43 loc) · 1.8 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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 constants_1 = require("@nestjs/core/constants");
const Observable_1 = require("rxjs/Observable");
const rpc_exception_1 = require("./rpc-exception");
require("rxjs/add/observable/throw");
class RpcExceptionsHandler {
constructor() {
this.filters = [];
}
handle(exception) {
const filterResult$ = this.invokeCustomFilters(exception);
if (filterResult$) {
return filterResult$;
}
const status = 'error';
if (!(exception instanceof rpc_exception_1.RpcException)) {
const message = constants_1.messages.UNKNOWN_EXCEPTION_MESSAGE;
return Observable_1.Observable.throw({ status, message });
}
const res = exception.getError();
const message = shared_utils_1.isObject(res) ? res : ({ status, message: res });
return Observable_1.Observable.throw(message);
}
setCustomFilters(filters) {
if (!Array.isArray(filters)) {
throw new invalid_exception_filter_exception_1.InvalidExceptionFilterException();
}
this.filters = filters;
}
invokeCustomFilters(exception) {
if (shared_utils_1.isEmpty(this.filters))
return null;
const filter = this.filters.find(({ exceptionMetatypes, func }) => {
const hasMetatype = !!exceptionMetatypes.find(ExceptionMetatype => exception instanceof ExceptionMetatype);
return hasMetatype;
});
return filter ? filter.func(exception) : null;
}
}
exports.RpcExceptionsHandler = RpcExceptionsHandler;