-
Notifications
You must be signed in to change notification settings - Fork 121
/
ErrorToJsonConverter.test.ts
164 lines (155 loc) · 6.57 KB
/
ErrorToJsonConverter.test.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { ErrorToJsonConverter } from '../../../../src/storage/conversion/ErrorToJsonConverter';
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
import { errorTermsToMetadata } from '../../../../src/util/errors/HttpErrorUtil';
import type { OAuthErrorFields } from '../../../../src/util/errors/OAuthHttpError';
import { OAuthHttpError } from '../../../../src/util/errors/OAuthHttpError';
import { readJsonStream } from '../../../../src/util/StreamUtil';
describe('An ErrorToJsonConverter', (): void => {
const identifier = { path: 'http://test.com/error' };
const converter = new ErrorToJsonConverter();
const preferences = {};
it('supports going from errors to json.', async(): Promise<void> => {
await expect(converter.getOutputTypes('internal/error')).resolves.toEqual({ 'application/json': 1 });
});
it('adds all HttpError fields.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text');
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'BadRequestHttpError',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
stack: error.stack,
details: {},
});
});
it('copies the HttpError details.', async(): Promise<void> => {
const metadata = errorTermsToMetadata({ important: 'detail' });
const error = new BadRequestHttpError('error text', { metadata });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'BadRequestHttpError',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
details: { important: 'detail' },
stack: error.stack,
});
});
it('adds OAuth fields if present.', async(): Promise<void> => {
const out: OAuthErrorFields = {
error: 'error',
// eslint-disable-next-line @typescript-eslint/naming-convention
error_description: 'error_description',
scope: 'scope',
state: 'state',
};
const error = new OAuthHttpError(out, 'InvalidRequest', 400, 'error text');
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'InvalidRequest',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
stack: error.stack,
error: 'error',
// eslint-disable-next-line @typescript-eslint/naming-convention
error_description: 'error_description',
scope: 'scope',
state: 'state',
details: {},
});
});
it('only adds stack if it is defined.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text');
delete error.stack;
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'BadRequestHttpError',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
details: {},
});
});
it('can handle non-error causes.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text', { cause: 'not an error' });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'BadRequestHttpError',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
stack: error.stack,
details: {},
cause: 'not an error',
});
});
it('ignores non-error causes that cannot be parsed.', async(): Promise<void> => {
const error = new BadRequestHttpError('error text', { cause: BigInt(5) });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'BadRequestHttpError',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
stack: error.stack,
details: {},
cause: {},
});
});
it('can handle non-HTTP errors as cause.', async(): Promise<void> => {
const cause = new Error('error');
const error = new BadRequestHttpError('error text', { cause });
const representation = new BasicRepresentation([ error ], 'internal/error', false);
const prom = converter.handle({ identifier, representation, preferences });
await expect(prom).resolves.toBeDefined();
const result = await prom;
expect(result.binary).toBe(true);
expect(result.metadata.contentType).toBe('application/json');
await expect(readJsonStream(result.data)).resolves.toEqual({
name: 'BadRequestHttpError',
message: 'error text',
statusCode: 400,
errorCode: 'H400',
stack: error.stack,
details: {},
cause: {
name: 'Error',
message: 'error',
stack: cause.stack,
},
});
});
});