Skip to content

Commit 978b540

Browse files
committed
refactor: disable catchall exception filter
1 parent 3a240d6 commit 978b540

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/common/decorators/base-endpoint/base-endpoint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function Endpoint(options: ApiEndpointOptions) {
5656
properties: {
5757
message: { type: 'string' },
5858
statusCode: { type: 'number', example: 400 },
59-
errors: { type: 'array', items: { type: 'object' } },
59+
error: { type: 'string' },
6060
},
6161
},
6262
}),

src/common/exception/catch-all-exception-filter.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import {
55
ArgumentsHost,
66
Catch,
77
ExceptionFilter,
8+
ForbiddenException,
89
HttpException,
910
HttpStatus,
1011
Logger,
12+
UnauthorizedException,
1113
} from '@nestjs/common';
1214

1315
import { HttpExceptionWithErrorCodeType } from './http-exeception-with-error-code.type';
@@ -16,25 +18,31 @@ import { HttpExceptionWithErrorCodeType } from './http-exeception-with-error-cod
1618
export class CatchAllExceptionFilter implements ExceptionFilter {
1719
private readonly logger = new Logger(CatchAllExceptionFilter.name);
1820

19-
catch(exception: HttpExceptionWithErrorCodeType | ZodValidationException, host: ArgumentsHost) {
21+
catch(exception: unknown, host: ArgumentsHost) {
2022
const ctx = host.switchToHttp();
2123
const response = ctx.getResponse<Response>();
2224
const request = ctx.getRequest<Request>();
2325

2426
let status = HttpStatus.INTERNAL_SERVER_ERROR;
25-
if (exception instanceof HttpException && exception.getStatus) {
27+
if (exception instanceof HttpException) {
2628
status = exception.getStatus();
2729
}
2830

29-
let errorMessage: string | string[];
31+
let errorMessage: string | string[] = 'Internal server error';
3032
let errorCode: string = 'E000';
33+
3134
if (status === HttpStatus.FORBIDDEN) {
3235
errorMessage = 'Forbidden';
33-
} else {
36+
} else if (exception instanceof Error) {
3437
errorMessage = exception.message;
35-
if (exception instanceof HttpExceptionWithErrorCodeType) {
36-
errorCode = exception.errorCode;
37-
}
38+
}
39+
40+
if (exception instanceof HttpExceptionWithErrorCodeType) {
41+
errorCode = exception.errorCode;
42+
} else if (exception instanceof UnauthorizedException) {
43+
errorCode = 'E401';
44+
} else if (exception instanceof ForbiddenException) {
45+
errorCode = 'E403';
3846
}
3947

4048
if (exception instanceof ZodValidationException) {
@@ -46,22 +54,29 @@ export class CatchAllExceptionFilter implements ExceptionFilter {
4654
});
4755

4856
response.status(status).json(exception.getResponse());
49-
} else if (exception instanceof HttpExceptionWithErrorCodeType) {
50-
response.status(status).json({
51-
timestamp: new Date().toISOString(),
52-
path: request.url,
53-
message: errorMessage,
54-
errorCode,
55-
});
56-
} else {
57-
this.logger.error(exception);
57+
return;
58+
}
5859

60+
if (
61+
exception instanceof HttpExceptionWithErrorCodeType ||
62+
exception instanceof HttpException
63+
) {
5964
response.status(status).json({
6065
timestamp: new Date().toISOString(),
6166
path: request.url,
6267
message: errorMessage,
6368
errorCode,
6469
});
70+
return;
6571
}
72+
73+
this.logger.error(exception);
74+
75+
response.status(status).json({
76+
timestamp: new Date().toISOString(),
77+
path: request.url,
78+
message: 'Internal server error',
79+
errorCode: 'E500',
80+
});
6681
}
6782
}

src/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { NestFactory } from '@nestjs/core';
1919

2020
import { getDocs, isCrowdinEditorEnabled, isDevelopment } from '@common/utils/startup-app';
2121
import { proxyCheckMiddleware, getRealIp, noRobotsMiddleware } from '@common/middlewares';
22-
import { CatchAllExceptionFilter } from '@common/exception/catch-all-exception-filter';
2322
import { getStartMessage } from '@common/utils/startup-app/get-start-message';
2423
import { customLogFilter } from '@common/utils/filter-logs';
2524
import { AxiosService } from '@common/axios';
@@ -138,7 +137,7 @@ async function bootstrap(): Promise<void> {
138137

139138
app.useGlobalPipes(new ZodValidationPipe());
140139

141-
app.useGlobalFilters(new CatchAllExceptionFilter());
140+
// app.useGlobalFilters(new CatchAllExceptionFilter());
142141

143142
app.enableShutdownHooks();
144143

0 commit comments

Comments
 (0)