-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-error.spec.ts
More file actions
209 lines (165 loc) · 6.14 KB
/
api-error.spec.ts
File metadata and controls
209 lines (165 loc) · 6.14 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { APIError } from '../src/lib/api-error';
describe('APIError', () => {
describe('constructor', () => {
it('should create an APIError with all properties', () => {
const error = new APIError('Test error', 'ERROR_CODE', 404);
expect(error.message).toBe('Test error');
expect(error.error_code).toBe('ERROR_CODE');
expect(error.status).toBe(404);
expect(error.error_message).toBe('Test error');
expect(error.name).toBe('APIError');
expect(error.stack).toBeUndefined();
});
it('should create an APIError with numeric error_code', () => {
const error = new APIError('Test error', 500, 500);
expect(error.error_code).toBe(500);
expect(error.status).toBe(500);
});
});
describe('fromAxiosError', () => {
it('should create APIError from axios error with response data', () => {
const axiosError = {
response: {
data: {
error_message: 'Not Found',
error_code: 404,
},
status: 404,
},
};
const error = APIError.fromAxiosError(axiosError);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Not Found');
expect(error.error_code).toBe(404);
expect(error.status).toBe(404);
});
it('should create APIError from axios error with message but no response', () => {
const axiosError = {
message: 'Network Error',
code: 'ENOTFOUND',
};
const error = APIError.fromAxiosError(axiosError);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Network Error');
expect(error.error_code).toBe('ENOTFOUND');
expect(error.status).toBe(0);
});
it('should create APIError from axios error with message but no code', () => {
const axiosError = {
message: 'Network Error',
};
const error = APIError.fromAxiosError(axiosError);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Network Error');
expect(error.error_code).toBe('NETWORK_ERROR');
expect(error.status).toBe(0);
});
it('should create APIError with default message for unknown errors', () => {
const axiosError = {};
const error = APIError.fromAxiosError(axiosError);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Unknown error occurred');
expect(error.error_code).toBe('UNKNOWN_ERROR');
expect(error.status).toBe(0);
});
it('should handle axios error with response.data but no response.status', () => {
const axiosError = {
response: {
data: {
error_message: 'Server Error',
},
},
};
// This should call fromResponseData, which requires status
// Let's test with a proper status
const axiosErrorWithStatus = {
response: {
data: {
error_message: 'Server Error',
},
status: 500,
},
};
const error = APIError.fromAxiosError(axiosErrorWithStatus);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Server Error');
expect(error.status).toBe(500);
});
});
describe('fromResponseData', () => {
it('should create APIError from response data with error_message', () => {
const responseData = {
error_message: 'Bad Request',
error_code: 400,
};
const error = APIError.fromResponseData(responseData, 400);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Bad Request');
expect(error.error_code).toBe(400);
expect(error.status).toBe(400);
});
it('should create APIError from response data with message fallback', () => {
const responseData = {
message: 'Internal Server Error',
code: 500,
};
const error = APIError.fromResponseData(responseData, 500);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Internal Server Error');
expect(error.error_code).toBe(500);
expect(error.status).toBe(500);
});
it('should create APIError from response data with error fallback', () => {
const responseData = {
error: 'Validation Error',
code: 422,
};
const error = APIError.fromResponseData(responseData, 422);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Validation Error');
expect(error.error_code).toBe(422);
expect(error.status).toBe(422);
});
it('should create APIError from string response data', () => {
const responseData = 'Plain text error message';
const error = APIError.fromResponseData(responseData, 500);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Plain text error message');
expect(error.error_code).toBe(500);
expect(error.status).toBe(500);
});
it('should create APIError with default message when no error fields present', () => {
const responseData = {
someOtherField: 'value',
};
const error = APIError.fromResponseData(responseData, 500);
expect(error).toBeInstanceOf(APIError);
expect(error.error_message).toBe('Request failed');
expect(error.error_code).toBe(500);
expect(error.status).toBe(500);
});
it('should extract error_code from response data', () => {
const responseData = {
error_message: 'Error',
error_code: 999,
};
const error = APIError.fromResponseData(responseData, 500);
expect(error.error_code).toBe(999);
});
it('should extract code from response data when error_code not present', () => {
const responseData = {
error_message: 'Error',
code: 888,
};
const error = APIError.fromResponseData(responseData, 500);
expect(error.error_code).toBe(888);
});
it('should use status as error_code fallback', () => {
const responseData = {
error_message: 'Error',
};
const error = APIError.fromResponseData(responseData, 503);
expect(error.error_code).toBe(503);
});
});
});