-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathexception.util.ts
More file actions
51 lines (39 loc) · 1.27 KB
/
exception.util.ts
File metadata and controls
51 lines (39 loc) · 1.27 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
import { HttpException, HttpStatus } from '@nestjs/common';
import { GraphQLErrorExtensions } from 'graphql';
import { GRAPHQL_ERROR_CODES } from './exception.constant';
import { BaseException } from './exception.factory';
export const isGraphqlOriginalError = (
extensions: GraphQLErrorExtensions,
): boolean => {
return (
typeof extensions?.code === 'string' &&
GRAPHQL_ERROR_CODES.includes(extensions.code)
);
};
export const isBaseException = (
error: unknown,
): error is BaseException<number, string, string> => {
return error instanceof BaseException;
};
export const isHttpException = (error: unknown): error is HttpException => {
return error instanceof HttpException;
};
export const getHttpExceptionCode = (status: number): string => {
return HttpStatus[status] || 'HTTP_ERROR';
};
export const getHttpExceptionMessage = (error: HttpException): string => {
const response = error.getResponse();
if (typeof response === 'string') {
return response;
}
if (typeof response === 'object' && response !== null) {
const { message } = response as { message?: string | string[] };
if (Array.isArray(message)) {
return message.join(', ');
}
if (typeof message === 'string') {
return message;
}
}
return error.message;
};