forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc-exceptions-handler.ts
More file actions
63 lines (56 loc) · 2.24 KB
/
Copy pathrpc-exceptions-handler.ts
File metadata and controls
63 lines (56 loc) · 2.24 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Logger } from '@nestjs/common';
import { RpcExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions';
import { ArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface';
import { isEmpty, isObject } from '@nestjs/common/utils/shared.utils';
import { messages } from '@nestjs/core/constants';
import { InvalidExceptionFilterException } from '@nestjs/core/errors/exceptions/invalid-exception-filter.exception';
import { Observable, throwError as _throw } from 'rxjs';
import { RpcException } from './rpc-exception';
export class RpcExceptionsHandler {
private static readonly logger = new Logger(RpcExceptionsHandler.name);
private filters: RpcExceptionFilterMetadata[] = [];
public handle(
exception: Error | RpcException | any,
host: ArgumentsHost,
): Observable<any> {
const filterResult$ = this.invokeCustomFilters(exception, host);
if (filterResult$) {
return filterResult$;
}
const status = 'error';
if (!(exception instanceof RpcException)) {
const errorMessage = messages.UNKNOWN_EXCEPTION_MESSAGE;
const isError = isObject(exception) && (exception as Error).message;
const loggerArgs = isError
? [(exception as Error).message, (exception as Error).stack]
: [exception];
const logger = RpcExceptionsHandler.logger;
logger.error.apply(logger, loggerArgs);
return _throw({ status, message: errorMessage });
}
const res = exception.getError();
const message = isObject(res) ? res : { status, message: res };
return _throw(message);
}
public setCustomFilters(filters: RpcExceptionFilterMetadata[]) {
if (!Array.isArray(filters)) {
throw new InvalidExceptionFilterException();
}
this.filters = filters;
}
public invokeCustomFilters(
exception,
host: ArgumentsHost,
): Observable<any> | null {
if (isEmpty(this.filters)) return null;
const filter = this.filters.find(({ exceptionMetatypes, func }) => {
const hasMetatype =
!exceptionMetatypes.length ||
!!exceptionMetatypes.find(
ExceptionMetatype => exception instanceof ExceptionMetatype,
);
return hasMetatype;
});
return filter ? filter.func(exception, host) : null;
}
}