Skip to content

Commit 5a618fe

Browse files
refactor(): extract handling unknown errors to separate functions
1 parent 7914322 commit 5a618fe

5 files changed

Lines changed: 46 additions & 27 deletions

File tree

packages/core/exceptions/base-exception-filter.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Optional,
1010
} from '@nestjs/common';
1111
import { isObject } from '@nestjs/common/utils/shared.utils';
12+
import { AbstractHttpAdapter } from '../adapters';
1213
import { MESSAGES } from '../constants';
1314
import { HttpAdapterHost } from '../helpers';
1415

@@ -27,18 +28,7 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
2728
(this.httpAdapterHost && this.httpAdapterHost.httpAdapter);
2829

2930
if (!(exception instanceof HttpException)) {
30-
const body = {
31-
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
32-
message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
33-
};
34-
applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
35-
if (this.isExceptionObject(exception)) {
36-
return BaseExceptionFilter.logger.error(
37-
exception.message,
38-
exception.stack,
39-
);
40-
}
41-
return BaseExceptionFilter.logger.error(exception);
31+
return this.handleUnknownError(exception, host, applicationRef);
4232
}
4333
const res = exception.getResponse();
4434
const message = isObject(res)
@@ -51,6 +41,25 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
5141
applicationRef.reply(host.getArgByIndex(1), message, exception.getStatus());
5242
}
5343

44+
public handleUnknownError(
45+
exception: T,
46+
host: ArgumentsHost,
47+
applicationRef: AbstractHttpAdapter | HttpServer,
48+
) {
49+
const body = {
50+
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
51+
message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
52+
};
53+
applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
54+
if (this.isExceptionObject(exception)) {
55+
return BaseExceptionFilter.logger.error(
56+
exception.message,
57+
exception.stack,
58+
);
59+
}
60+
return BaseExceptionFilter.logger.error(exception);
61+
}
62+
5463
public isExceptionObject(err: any): err is Error {
5564
return isObject(err) && !!(err as Error).message;
5665
}

packages/core/injector/module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,12 @@ export class Module {
281281
provider: ClassProvider & ProviderName,
282282
collection: Map<string, InstanceWrapper>,
283283
) {
284-
const { name, useClass, scope } = provider;
284+
const { name, useClass } = provider;
285+
286+
let { scope } = provider;
287+
if (isUndefined(scope)) {
288+
scope = this.getClassScope(useClass);
289+
}
285290
collection.set(
286291
name as string,
287292
new InstanceWrapper({
@@ -501,7 +506,7 @@ export class Module {
501506
};
502507
}
503508

504-
private getClassScope(provider: Provider): Scope {
509+
private getClassScope(provider: Type<unknown>): Scope {
505510
const metadata = Reflect.getMetadata(SCOPE_OPTIONS_METADATA, provider);
506511
return metadata && metadata.scope;
507512
}

packages/microservices/exceptions/base-rpc-exception-filter.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,29 @@ export class BaseRpcExceptionFilter<T = any, R = any>
88
implements RpcExceptionFilter<T> {
99
private static readonly logger = new Logger('RpcExceptionsHandler');
1010

11-
catch(exception: T, host: ArgumentsHost): Observable<R> {
11+
public catch(exception: T, host: ArgumentsHost): Observable<R> {
1212
const status = 'error';
1313
if (!(exception instanceof RpcException)) {
14-
const errorMessage = MESSAGES.UNKNOWN_EXCEPTION_MESSAGE;
15-
16-
const loggerArgs = this.isError(exception)
17-
? [exception.message, exception.stack]
18-
: [exception];
19-
const logger = BaseRpcExceptionFilter.logger;
20-
logger.error.apply(logger, loggerArgs as any);
21-
22-
return _throw({ status, message: errorMessage });
14+
return this.handleUnknownError(exception);
2315
}
2416
const res = exception.getError();
2517
const message = isObject(res) ? res : { status, message: res };
2618
return _throw(message);
2719
}
2820

29-
isError(exception: any): exception is Error {
21+
public handleUnknownError(exception: T) {
22+
const errorMessage = MESSAGES.UNKNOWN_EXCEPTION_MESSAGE;
23+
24+
const loggerArgs = this.isError(exception)
25+
? [exception.message, exception.stack]
26+
: [exception];
27+
const logger = BaseRpcExceptionFilter.logger;
28+
logger.error.apply(logger, loggerArgs as any);
29+
30+
return _throw({ status, message: errorMessage });
31+
}
32+
33+
public isError(exception: any): exception is Error {
3034
return !!(isObject(exception) && (exception as Error).message);
3135
}
3236
}

packages/microservices/exceptions/rpc-exception.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { isString } from '@nestjs/common/utils/shared.utils';
22

33
export class RpcException extends Error {
44
public readonly message: any;
5+
56
constructor(private readonly error: string | object) {
67
super();
78
this.message = error;

packages/websockets/exceptions/base-ws-exception-filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { MESSAGES } from '@nestjs/core/constants';
44
import { WsException } from '../errors/ws-exception';
55

66
export class BaseWsExceptionFilter<T = any> implements WsExceptionFilter<T> {
7-
catch(exception: T, host: ArgumentsHost) {
7+
public catch(exception: T, host: ArgumentsHost) {
88
const client = host.switchToWs().getClient();
99
this.handleError(client, exception);
1010
}
1111

12-
handleError<IClient extends { emit: Function }>(
12+
public handleError<IClient extends { emit: Function }>(
1313
client: IClient,
1414
exception: T,
1515
) {

0 commit comments

Comments
 (0)