@nest-yalc-2/errors 2.2.1 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +16 -8
- package/src/__tests__/database-exception.filter.spec.ts +71 -71
- package/src/__tests__/http-exception.filter.spec.ts +182 -182
- package/src/__tests__/system-exception.filter.spec.ts +39 -39
- package/src/__tests__/validation-exception.filter.spec.ts +31 -31
- package/src/filters/database-exception.filter.ts +45 -45
- package/src/filters/http-exception.filter.ts +144 -144
- package/src/filters/index.ts +6 -6
- package/src/filters/system-exception.filter.ts +13 -13
- package/src/filters/validation-exception.filter.ts +26 -26
- package/src/index.ts +8 -8
- package/src/__tests__/default.error.spec.ts +0 -529
- package/src/__tests__/error.helper.spec.ts +0 -23
- package/src/__tests__/errors.enum.spec.ts +0 -32
- package/src/__tests__/http-status-code-to-errors.spec.ts +0 -26
- package/src/__tests__/input-validation.error.spec.ts +0 -27
- package/src/__tests__/login.error.spec.ts +0 -23
- package/src/__tests__/result.error.spec.ts +0 -47
- package/src/__tests__/verification.error.spec.ts +0 -28
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nest-yalc-2/errors",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -16,17 +16,25 @@
|
|
|
16
16
|
"src"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@nest-yalc-2/crud-gen": "^2.2.
|
|
20
|
-
"@nest-yalc-2/event-manager": "^2.2.
|
|
21
|
-
"@nest-yalc-2/logger": "^2.2.
|
|
22
|
-
"@nest-yalc-2/utils": "^2.2.
|
|
23
|
-
"axios": "^1.
|
|
19
|
+
"@nest-yalc-2/crud-gen": "^2.2.3",
|
|
20
|
+
"@nest-yalc-2/event-manager": "^2.2.3",
|
|
21
|
+
"@nest-yalc-2/logger": "^2.2.3",
|
|
22
|
+
"@nest-yalc-2/utils": "^2.2.3",
|
|
23
|
+
"axios": "^1.20.0",
|
|
24
24
|
"neverthrow": "^8.2.0",
|
|
25
|
-
"tslib": "^2.
|
|
25
|
+
"tslib": "^2.8.1"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@nestjs/common": "^
|
|
28
|
+
"@nestjs/common": "^12.0.0"
|
|
29
29
|
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"nestjs",
|
|
32
|
+
"nestjs-yalc",
|
|
33
|
+
"yalc",
|
|
34
|
+
"errors",
|
|
35
|
+
"framework",
|
|
36
|
+
"utility"
|
|
37
|
+
],
|
|
30
38
|
"repository": {
|
|
31
39
|
"type": "git",
|
|
32
40
|
"url": "git+https://github.com/Nek97/nestjs-yalc.git"
|
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import {
|
|
2
|
-
expect,
|
|
3
|
-
jest,
|
|
4
|
-
describe,
|
|
5
|
-
it,
|
|
6
|
-
beforeEach,
|
|
7
|
-
beforeAll,
|
|
8
|
-
afterAll,
|
|
9
|
-
afterEach,
|
|
10
|
-
} from '@jest/globals';
|
|
11
|
-
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
-
import {
|
|
13
|
-
ArgumentsHost,
|
|
14
|
-
InternalServerErrorException,
|
|
15
|
-
LoggerService,
|
|
16
|
-
} from '@nestjs/common';
|
|
17
|
-
import { EntityTarget } from 'typeorm';
|
|
18
|
-
import { ConnectionNotFoundError, EntityNotFoundError } from 'typeorm';
|
|
19
|
-
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
20
|
-
import { DatabaseExceptionFilter } from '../filters/database-exception.filter.js';
|
|
21
|
-
|
|
22
|
-
import { GqlArgumentsHost } from '@nestjs/graphql';
|
|
23
|
-
|
|
24
|
-
jest.mock('@nestjs/graphql');
|
|
25
|
-
|
|
26
|
-
describe('Database exceptions filter', () => {
|
|
27
|
-
let filter: DatabaseExceptionFilter;
|
|
28
|
-
const loggerServiceMock = createMock<LoggerService>();
|
|
29
|
-
|
|
30
|
-
beforeEach(() => {
|
|
31
|
-
filter = new DatabaseExceptionFilter(loggerServiceMock);
|
|
32
|
-
GqlArgumentsHost.create = jest.fn().mockReturnValue({
|
|
33
|
-
getType: jest.fn(),
|
|
34
|
-
} as any) as any;
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
afterEach(() => {
|
|
38
|
-
jest.restoreAllMocks();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should be defined', () => {
|
|
42
|
-
expect(filter).toBeDefined();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should catch and log EntityNotFound errors', () => {
|
|
46
|
-
const mockArgumentsHost = createMock<ArgumentsHost>();
|
|
47
|
-
|
|
48
|
-
const mockedEntityTarget: EntityTarget<null> = 'emptyString';
|
|
49
|
-
const error = new EntityNotFoundError(mockedEntityTarget, []);
|
|
50
|
-
|
|
51
|
-
const returnedError = filter.catch(error, mockArgumentsHost);
|
|
52
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
53
|
-
error,
|
|
54
|
-
ExceptionContextEnum.DATABASE,
|
|
55
|
-
);
|
|
56
|
-
expect(returnedError).toBeInstanceOf(InternalServerErrorException);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('should catch and log ConnectionNotFoundError errors', () => {
|
|
60
|
-
const mockArgumentsHost = createMock<ArgumentsHost>();
|
|
61
|
-
|
|
62
|
-
const error = new ConnectionNotFoundError('test connection');
|
|
63
|
-
|
|
64
|
-
filter.catch(error, mockArgumentsHost);
|
|
65
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
66
|
-
error,
|
|
67
|
-
error.stack,
|
|
68
|
-
ExceptionContextEnum.DATABASE,
|
|
69
|
-
);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
jest,
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
beforeEach,
|
|
7
|
+
beforeAll,
|
|
8
|
+
afterAll,
|
|
9
|
+
afterEach,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
+
import {
|
|
13
|
+
ArgumentsHost,
|
|
14
|
+
InternalServerErrorException,
|
|
15
|
+
LoggerService,
|
|
16
|
+
} from '@nestjs/common';
|
|
17
|
+
import { EntityTarget } from 'typeorm';
|
|
18
|
+
import { ConnectionNotFoundError, EntityNotFoundError } from 'typeorm';
|
|
19
|
+
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
20
|
+
import { DatabaseExceptionFilter } from '../filters/database-exception.filter.js';
|
|
21
|
+
|
|
22
|
+
import { GqlArgumentsHost } from '@nestjs/graphql';
|
|
23
|
+
|
|
24
|
+
jest.mock('@nestjs/graphql');
|
|
25
|
+
|
|
26
|
+
describe('Database exceptions filter', () => {
|
|
27
|
+
let filter: DatabaseExceptionFilter;
|
|
28
|
+
const loggerServiceMock = createMock<LoggerService>();
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
filter = new DatabaseExceptionFilter(loggerServiceMock);
|
|
32
|
+
GqlArgumentsHost.create = jest.fn().mockReturnValue({
|
|
33
|
+
getType: jest.fn(),
|
|
34
|
+
} as any) as any;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
jest.restoreAllMocks();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should be defined', () => {
|
|
42
|
+
expect(filter).toBeDefined();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should catch and log EntityNotFound errors', () => {
|
|
46
|
+
const mockArgumentsHost = createMock<ArgumentsHost>();
|
|
47
|
+
|
|
48
|
+
const mockedEntityTarget: EntityTarget<null> = 'emptyString';
|
|
49
|
+
const error = new EntityNotFoundError(mockedEntityTarget, []);
|
|
50
|
+
|
|
51
|
+
const returnedError = filter.catch(error, mockArgumentsHost);
|
|
52
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
53
|
+
error,
|
|
54
|
+
ExceptionContextEnum.DATABASE,
|
|
55
|
+
);
|
|
56
|
+
expect(returnedError).toBeInstanceOf(InternalServerErrorException);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should catch and log ConnectionNotFoundError errors', () => {
|
|
60
|
+
const mockArgumentsHost = createMock<ArgumentsHost>();
|
|
61
|
+
|
|
62
|
+
const error = new ConnectionNotFoundError('test connection');
|
|
63
|
+
|
|
64
|
+
filter.catch(error, mockArgumentsHost);
|
|
65
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
66
|
+
error,
|
|
67
|
+
error.stack,
|
|
68
|
+
ExceptionContextEnum.DATABASE,
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -1,182 +1,182 @@
|
|
|
1
|
-
import { expect, jest, describe, it, beforeEach } from '@jest/globals';
|
|
2
|
-
|
|
3
|
-
import { DeepMocked, createMock } from '@golevelup/ts-jest';
|
|
4
|
-
import { CreateEntityError } from '@nest-yalc-2/crud-gen/entity.error.js';
|
|
5
|
-
import { MissingArgumentsError } from '@nest-yalc-2/crud-gen/missing-arguments.error.js';
|
|
6
|
-
import {
|
|
7
|
-
ArgumentsHost,
|
|
8
|
-
BadRequestException,
|
|
9
|
-
InternalServerErrorException,
|
|
10
|
-
LoggerService,
|
|
11
|
-
} from '@nestjs/common';
|
|
12
|
-
import { HttpExceptionFilter } from '../filters/http-exception.filter.js';
|
|
13
|
-
import { GqlError } from '@nest-yalc-2/graphql/plugins/gql.error.js';
|
|
14
|
-
import { DefaultError } from '../index.js';
|
|
15
|
-
import { GqlArgumentsHost } from '@nestjs/graphql';
|
|
16
|
-
describe('Http exceptions filter', () => {
|
|
17
|
-
let filter: HttpExceptionFilter;
|
|
18
|
-
let loggerServiceMock: DeepMocked<LoggerService>;
|
|
19
|
-
let mockArgumentsHost: DeepMocked<ArgumentsHost>;
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
jest.resetAllMocks();
|
|
23
|
-
loggerServiceMock = createMock<LoggerService>();
|
|
24
|
-
mockArgumentsHost = createMock<ArgumentsHost>();
|
|
25
|
-
filter = new HttpExceptionFilter(loggerServiceMock);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
afterEach(() => {
|
|
29
|
-
jest.restoreAllMocks();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should be defined', () => {
|
|
33
|
-
expect(filter).toBeDefined();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should catch and log Application errors', () => {
|
|
37
|
-
const error = new MissingArgumentsError();
|
|
38
|
-
|
|
39
|
-
filter.catch(error, mockArgumentsHost);
|
|
40
|
-
expect(loggerServiceMock.log).toHaveBeenCalledWith(
|
|
41
|
-
error.message,
|
|
42
|
-
expect.anything(),
|
|
43
|
-
);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('should catch Default error', () => {
|
|
47
|
-
const exception = new DefaultError('test');
|
|
48
|
-
|
|
49
|
-
filter.catch(exception, mockArgumentsHost);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('should catch and log System Error error', () => {
|
|
53
|
-
const exception = new Error('test');
|
|
54
|
-
|
|
55
|
-
filter.catch(exception, mockArgumentsHost);
|
|
56
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
57
|
-
exception.message,
|
|
58
|
-
exception.stack,
|
|
59
|
-
expect.anything(),
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should catch and log Entity error', () => {
|
|
64
|
-
const exception = new CreateEntityError();
|
|
65
|
-
|
|
66
|
-
filter.catch(exception, mockArgumentsHost);
|
|
67
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
68
|
-
exception,
|
|
69
|
-
undefined,
|
|
70
|
-
expect.anything(),
|
|
71
|
-
);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('should catch EntityError and log the original message', () => {
|
|
75
|
-
const fixedError = new Error('message');
|
|
76
|
-
const exception = new CreateEntityError(fixedError);
|
|
77
|
-
|
|
78
|
-
filter.catch(exception, mockArgumentsHost);
|
|
79
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
80
|
-
fixedError.message,
|
|
81
|
-
expect.anything(),
|
|
82
|
-
expect.objectContaining({
|
|
83
|
-
data: expect.objectContaining({
|
|
84
|
-
name: 'CreateEntityError',
|
|
85
|
-
}),
|
|
86
|
-
}),
|
|
87
|
-
);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('should catch and log GqlError error', () => {
|
|
91
|
-
const exception: GqlError = new GqlError('message', 'systemMessage');
|
|
92
|
-
filter.catch(exception, mockArgumentsHost);
|
|
93
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(exception.systemMessage);
|
|
94
|
-
|
|
95
|
-
exception.systemMessage = undefined;
|
|
96
|
-
filter.catch(exception, mockArgumentsHost);
|
|
97
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(exception.message);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should catch and log Http error with host type http and send response', () => {
|
|
101
|
-
const exception = new InternalServerErrorException();
|
|
102
|
-
mockArgumentsHost.getType.mockReturnValue('http');
|
|
103
|
-
const mockResponse = {
|
|
104
|
-
status: jest.fn().mockReturnThis(),
|
|
105
|
-
send: jest.fn(),
|
|
106
|
-
};
|
|
107
|
-
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
108
|
-
getResponse: () => mockResponse,
|
|
109
|
-
getRequest: jest.fn(),
|
|
110
|
-
getNext: jest.fn(),
|
|
111
|
-
} as any);
|
|
112
|
-
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
113
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
114
|
-
exception.message,
|
|
115
|
-
exception.stack,
|
|
116
|
-
expect.anything(),
|
|
117
|
-
);
|
|
118
|
-
expect(mockResponse.status).toHaveBeenCalledWith(500);
|
|
119
|
-
expect(mockResponse.send).toHaveBeenCalledWith(exception.message);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should catch and log Non-Http error with host type http and send response', () => {
|
|
123
|
-
const exception = new Error('test');
|
|
124
|
-
mockArgumentsHost.getType.mockReturnValue('http');
|
|
125
|
-
const mockResponse = {
|
|
126
|
-
status: jest.fn().mockReturnThis(),
|
|
127
|
-
send: jest.fn(),
|
|
128
|
-
};
|
|
129
|
-
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
130
|
-
getResponse: () => mockResponse,
|
|
131
|
-
getRequest: jest.fn(),
|
|
132
|
-
getNext: jest.fn(),
|
|
133
|
-
} as any);
|
|
134
|
-
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
135
|
-
expect(mockResponse.status).toHaveBeenCalledWith(500);
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('should not send response if sendResponse is false', () => {
|
|
139
|
-
const exception = new InternalServerErrorException();
|
|
140
|
-
mockArgumentsHost.getType.mockReturnValue('http');
|
|
141
|
-
const mockResponse = {
|
|
142
|
-
status: jest.fn().mockReturnThis(),
|
|
143
|
-
send: jest.fn(),
|
|
144
|
-
};
|
|
145
|
-
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
146
|
-
getResponse: () => mockResponse,
|
|
147
|
-
getRequest: jest.fn(),
|
|
148
|
-
getNext: jest.fn(),
|
|
149
|
-
} as any);
|
|
150
|
-
filter.catch(exception, mockArgumentsHost as ArgumentsHost, { sendResponse: false });
|
|
151
|
-
expect(mockResponse.status).not.toHaveBeenCalled();
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it('should catch and log Http log with host type http', () => {
|
|
155
|
-
const exception = new BadRequestException();
|
|
156
|
-
mockArgumentsHost.getType.mockReturnValue('http');
|
|
157
|
-
const mockResponse = {
|
|
158
|
-
status: jest.fn().mockReturnThis(),
|
|
159
|
-
send: jest.fn(),
|
|
160
|
-
};
|
|
161
|
-
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
162
|
-
getResponse: () => mockResponse,
|
|
163
|
-
getRequest: jest.fn(),
|
|
164
|
-
getNext: jest.fn(),
|
|
165
|
-
} as any);
|
|
166
|
-
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
167
|
-
expect(loggerServiceMock.log).toHaveBeenCalledWith(
|
|
168
|
-
exception.message,
|
|
169
|
-
expect.anything(),
|
|
170
|
-
);
|
|
171
|
-
expect(mockResponse.status).toHaveBeenCalledWith(400);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should catch an error with the logger itself', () => {
|
|
175
|
-
const exception = new InternalServerErrorException();
|
|
176
|
-
loggerServiceMock.error = null;
|
|
177
|
-
mockArgumentsHost.getType.mockReturnValue('http');
|
|
178
|
-
console.error = jest.fn();
|
|
179
|
-
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
180
|
-
expect(console.error).toHaveBeenCalled();
|
|
181
|
-
});
|
|
182
|
-
});
|
|
1
|
+
import { expect, jest, describe, it, beforeEach } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import { DeepMocked, createMock } from '@golevelup/ts-jest';
|
|
4
|
+
import { CreateEntityError } from '@nest-yalc-2/crud-gen/entity.error.js';
|
|
5
|
+
import { MissingArgumentsError } from '@nest-yalc-2/crud-gen/missing-arguments.error.js';
|
|
6
|
+
import {
|
|
7
|
+
ArgumentsHost,
|
|
8
|
+
BadRequestException,
|
|
9
|
+
InternalServerErrorException,
|
|
10
|
+
LoggerService,
|
|
11
|
+
} from '@nestjs/common';
|
|
12
|
+
import { HttpExceptionFilter } from '../filters/http-exception.filter.js';
|
|
13
|
+
import { GqlError } from '@nest-yalc-2/graphql/plugins/gql.error.js';
|
|
14
|
+
import { DefaultError } from '../index.js';
|
|
15
|
+
import { GqlArgumentsHost } from '@nestjs/graphql';
|
|
16
|
+
describe('Http exceptions filter', () => {
|
|
17
|
+
let filter: HttpExceptionFilter;
|
|
18
|
+
let loggerServiceMock: DeepMocked<LoggerService>;
|
|
19
|
+
let mockArgumentsHost: DeepMocked<ArgumentsHost>;
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
jest.resetAllMocks();
|
|
23
|
+
loggerServiceMock = createMock<LoggerService>();
|
|
24
|
+
mockArgumentsHost = createMock<ArgumentsHost>();
|
|
25
|
+
filter = new HttpExceptionFilter(loggerServiceMock);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
jest.restoreAllMocks();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should be defined', () => {
|
|
33
|
+
expect(filter).toBeDefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should catch and log Application errors', () => {
|
|
37
|
+
const error = new MissingArgumentsError();
|
|
38
|
+
|
|
39
|
+
filter.catch(error, mockArgumentsHost);
|
|
40
|
+
expect(loggerServiceMock.log).toHaveBeenCalledWith(
|
|
41
|
+
error.message,
|
|
42
|
+
expect.anything(),
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should catch Default error', () => {
|
|
47
|
+
const exception = new DefaultError('test');
|
|
48
|
+
|
|
49
|
+
filter.catch(exception, mockArgumentsHost);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should catch and log System Error error', () => {
|
|
53
|
+
const exception = new Error('test');
|
|
54
|
+
|
|
55
|
+
filter.catch(exception, mockArgumentsHost);
|
|
56
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
57
|
+
exception.message,
|
|
58
|
+
exception.stack,
|
|
59
|
+
expect.anything(),
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should catch and log Entity error', () => {
|
|
64
|
+
const exception = new CreateEntityError();
|
|
65
|
+
|
|
66
|
+
filter.catch(exception, mockArgumentsHost);
|
|
67
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
68
|
+
exception,
|
|
69
|
+
undefined,
|
|
70
|
+
expect.anything(),
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should catch EntityError and log the original message', () => {
|
|
75
|
+
const fixedError = new Error('message');
|
|
76
|
+
const exception = new CreateEntityError(fixedError);
|
|
77
|
+
|
|
78
|
+
filter.catch(exception, mockArgumentsHost);
|
|
79
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
80
|
+
fixedError.message,
|
|
81
|
+
expect.anything(),
|
|
82
|
+
expect.objectContaining({
|
|
83
|
+
data: expect.objectContaining({
|
|
84
|
+
name: 'CreateEntityError',
|
|
85
|
+
}),
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should catch and log GqlError error', () => {
|
|
91
|
+
const exception: GqlError = new GqlError('message', 'systemMessage');
|
|
92
|
+
filter.catch(exception, mockArgumentsHost);
|
|
93
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(exception.systemMessage);
|
|
94
|
+
|
|
95
|
+
exception.systemMessage = undefined;
|
|
96
|
+
filter.catch(exception, mockArgumentsHost);
|
|
97
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(exception.message);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should catch and log Http error with host type http and send response', () => {
|
|
101
|
+
const exception = new InternalServerErrorException();
|
|
102
|
+
mockArgumentsHost.getType.mockReturnValue('http');
|
|
103
|
+
const mockResponse = {
|
|
104
|
+
status: jest.fn().mockReturnThis(),
|
|
105
|
+
send: jest.fn(),
|
|
106
|
+
};
|
|
107
|
+
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
108
|
+
getResponse: () => mockResponse,
|
|
109
|
+
getRequest: jest.fn(),
|
|
110
|
+
getNext: jest.fn(),
|
|
111
|
+
} as any);
|
|
112
|
+
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
113
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
114
|
+
exception.message,
|
|
115
|
+
exception.stack,
|
|
116
|
+
expect.anything(),
|
|
117
|
+
);
|
|
118
|
+
expect(mockResponse.status).toHaveBeenCalledWith(500);
|
|
119
|
+
expect(mockResponse.send).toHaveBeenCalledWith(exception.message);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should catch and log Non-Http error with host type http and send response', () => {
|
|
123
|
+
const exception = new Error('test');
|
|
124
|
+
mockArgumentsHost.getType.mockReturnValue('http');
|
|
125
|
+
const mockResponse = {
|
|
126
|
+
status: jest.fn().mockReturnThis(),
|
|
127
|
+
send: jest.fn(),
|
|
128
|
+
};
|
|
129
|
+
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
130
|
+
getResponse: () => mockResponse,
|
|
131
|
+
getRequest: jest.fn(),
|
|
132
|
+
getNext: jest.fn(),
|
|
133
|
+
} as any);
|
|
134
|
+
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
135
|
+
expect(mockResponse.status).toHaveBeenCalledWith(500);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should not send response if sendResponse is false', () => {
|
|
139
|
+
const exception = new InternalServerErrorException();
|
|
140
|
+
mockArgumentsHost.getType.mockReturnValue('http');
|
|
141
|
+
const mockResponse = {
|
|
142
|
+
status: jest.fn().mockReturnThis(),
|
|
143
|
+
send: jest.fn(),
|
|
144
|
+
};
|
|
145
|
+
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
146
|
+
getResponse: () => mockResponse,
|
|
147
|
+
getRequest: jest.fn(),
|
|
148
|
+
getNext: jest.fn(),
|
|
149
|
+
} as any);
|
|
150
|
+
filter.catch(exception, mockArgumentsHost as ArgumentsHost, { sendResponse: false });
|
|
151
|
+
expect(mockResponse.status).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should catch and log Http log with host type http', () => {
|
|
155
|
+
const exception = new BadRequestException();
|
|
156
|
+
mockArgumentsHost.getType.mockReturnValue('http');
|
|
157
|
+
const mockResponse = {
|
|
158
|
+
status: jest.fn().mockReturnThis(),
|
|
159
|
+
send: jest.fn(),
|
|
160
|
+
};
|
|
161
|
+
mockArgumentsHost.switchToHttp.mockReturnValue({
|
|
162
|
+
getResponse: () => mockResponse,
|
|
163
|
+
getRequest: jest.fn(),
|
|
164
|
+
getNext: jest.fn(),
|
|
165
|
+
} as any);
|
|
166
|
+
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
167
|
+
expect(loggerServiceMock.log).toHaveBeenCalledWith(
|
|
168
|
+
exception.message,
|
|
169
|
+
expect.anything(),
|
|
170
|
+
);
|
|
171
|
+
expect(mockResponse.status).toHaveBeenCalledWith(400);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should catch an error with the logger itself', () => {
|
|
175
|
+
const exception = new InternalServerErrorException();
|
|
176
|
+
loggerServiceMock.error = null;
|
|
177
|
+
mockArgumentsHost.getType.mockReturnValue('http');
|
|
178
|
+
console.error = jest.fn();
|
|
179
|
+
filter.catch(exception, mockArgumentsHost as ArgumentsHost);
|
|
180
|
+
expect(console.error).toHaveBeenCalled();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import {
|
|
2
|
-
expect,
|
|
3
|
-
jest,
|
|
4
|
-
describe,
|
|
5
|
-
it,
|
|
6
|
-
beforeEach,
|
|
7
|
-
beforeAll,
|
|
8
|
-
afterAll,
|
|
9
|
-
afterEach,
|
|
10
|
-
} from '@jest/globals';
|
|
11
|
-
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
-
import { LoggerService, UnprocessableEntityException } from '@nestjs/common';
|
|
13
|
-
|
|
14
|
-
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
15
|
-
import { SystemExceptionFilter } from '../filters/system-exception.filter.js';
|
|
16
|
-
|
|
17
|
-
describe('System exceptions filter', () => {
|
|
18
|
-
let filter: SystemExceptionFilter;
|
|
19
|
-
const loggerServiceMock = createMock<LoggerService>();
|
|
20
|
-
|
|
21
|
-
beforeAll(() => {
|
|
22
|
-
filter = new SystemExceptionFilter(loggerServiceMock);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('should be defined', () => {
|
|
26
|
-
expect(filter).toBeDefined();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should catch and log UnprocessableEntity error', () => {
|
|
30
|
-
const exception = new UnprocessableEntityException();
|
|
31
|
-
|
|
32
|
-
filter.catch(exception);
|
|
33
|
-
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
34
|
-
exception,
|
|
35
|
-
exception.stack,
|
|
36
|
-
ExceptionContextEnum.SYSTEM,
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
jest,
|
|
4
|
+
describe,
|
|
5
|
+
it,
|
|
6
|
+
beforeEach,
|
|
7
|
+
beforeAll,
|
|
8
|
+
afterAll,
|
|
9
|
+
afterEach,
|
|
10
|
+
} from '@jest/globals';
|
|
11
|
+
import { createMock } from '@golevelup/ts-jest';
|
|
12
|
+
import { LoggerService, UnprocessableEntityException } from '@nestjs/common';
|
|
13
|
+
|
|
14
|
+
import { ExceptionContextEnum } from '@node-yalc/errors/error.enum.js';
|
|
15
|
+
import { SystemExceptionFilter } from '../filters/system-exception.filter.js';
|
|
16
|
+
|
|
17
|
+
describe('System exceptions filter', () => {
|
|
18
|
+
let filter: SystemExceptionFilter;
|
|
19
|
+
const loggerServiceMock = createMock<LoggerService>();
|
|
20
|
+
|
|
21
|
+
beforeAll(() => {
|
|
22
|
+
filter = new SystemExceptionFilter(loggerServiceMock);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should be defined', () => {
|
|
26
|
+
expect(filter).toBeDefined();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should catch and log UnprocessableEntity error', () => {
|
|
30
|
+
const exception = new UnprocessableEntityException();
|
|
31
|
+
|
|
32
|
+
filter.catch(exception);
|
|
33
|
+
expect(loggerServiceMock.error).toHaveBeenCalledWith(
|
|
34
|
+
exception,
|
|
35
|
+
exception.stack,
|
|
36
|
+
ExceptionContextEnum.SYSTEM,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
});
|