Skip to content

Commit bfbb6d2

Browse files
committed
Fixed tets cases
1 parent 280e96f commit bfbb6d2

14 files changed

+54
-25
lines changed
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
24
const errors: Record<number, string> = {
35
400: 'Bad Request',
@@ -16,16 +18,18 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
1618
}
1719

1820
if (!result.ok) {
19-
const resultBody = (() => {
20-
try {
21-
return JSON.stringify(result.body, null, 2);
22-
} catch (err) {
23-
return undefined;
24-
}
25-
})();
26-
throw new ApiError(
27-
result,
28-
`Generic Error: status: ${result.status ?? 'unknown'}; status text: ${result.statusText}; body: ${resultBody}`
29-
);
21+
const errorStatus = result.status ?? 'unknown';
22+
const errorStatusText = result.statusText ?? 'unknown';
23+
const errorBody = (() => {
24+
try {
25+
return JSON.stringify(result.body, null, 2);
26+
} catch (e) {
27+
return undefined;
28+
}
29+
})();
30+
31+
throw new ApiError(options, result,
32+
`Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`
33+
);
3034
}
3135
};

test/__snapshots__/index.spec.ts.snap

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,19 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
498498
}
499499

500500
if (!result.ok) {
501-
throw new ApiError(options, result, 'Generic Error');
501+
const errorStatus = result.status ?? 'unknown';
502+
const errorStatusText = result.statusText ?? 'unknown';
503+
const errorBody = (() => {
504+
try {
505+
return JSON.stringify(result.body, null, 2);
506+
} catch (e) {
507+
return undefined;
508+
}
509+
})();
510+
511+
throw new ApiError(options, result,
512+
\`Generic Error: status: \${errorStatus}; status text: \${errorStatusText}; body: \${errorBody}\`
513+
);
502514
}
503515
};
504516

@@ -3593,7 +3605,19 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
35933605
}
35943606

35953607
if (!result.ok) {
3596-
throw new ApiError(options, result, 'Generic Error');
3608+
const errorStatus = result.status ?? 'unknown';
3609+
const errorStatusText = result.statusText ?? 'unknown';
3610+
const errorBody = (() => {
3611+
try {
3612+
return JSON.stringify(result.body, null, 2);
3613+
} catch (e) {
3614+
return undefined;
3615+
}
3616+
})();
3617+
3618+
throw new ApiError(options, result,
3619+
\`Generic Error: status: \${errorStatus}; status text: \${errorStatusText}; body: \${errorBody}\`
3620+
);
35973621
}
35983622
};
35993623

test/e2e/client.angular.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ describe('client.angular', () => {
139139
});
140140
});
141141
});
142+
142143
expect(error).toBe(
143144
JSON.stringify({
144145
name: 'ApiError',
145-
message: 'Generic Error',
146+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
146147
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
147148
status: 409,
148149
statusText: 'Conflict',

test/e2e/client.axios.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('client.axios', () => {
136136
expect(error).toBe(
137137
JSON.stringify({
138138
name: 'ApiError',
139-
message: 'Generic Error',
139+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
140140
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
141141
status: 409,
142142
statusText: 'Conflict',

test/e2e/client.babel.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('client.babel', () => {
161161
expect(error).toBe(
162162
JSON.stringify({
163163
name: 'ApiError',
164-
message: 'Generic Error',
164+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
165165
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
166166
status: 409,
167167
statusText: 'Conflict',

test/e2e/client.fetch.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('client.fetch', () => {
156156
expect(error).toBe(
157157
JSON.stringify({
158158
name: 'ApiError',
159-
message: 'Generic Error',
159+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
160160
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
161161
status: 409,
162162
statusText: 'Conflict',

test/e2e/client.node.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('client.node', () => {
136136
expect(error).toBe(
137137
JSON.stringify({
138138
name: 'ApiError',
139-
message: 'Generic Error',
139+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
140140
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
141141
status: 409,
142142
statusText: 'Conflict',

test/e2e/client.xhr.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('client.xhr', () => {
155155
expect(error).toBe(
156156
JSON.stringify({
157157
name: 'ApiError',
158-
message: 'Generic Error',
158+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
159159
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
160160
status: 409,
161161
statusText: 'Conflict',

test/e2e/v3.angular.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('v3.angular', () => {
144144
expect(error).toBe(
145145
JSON.stringify({
146146
name: 'ApiError',
147-
message: 'Generic Error',
147+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
148148
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
149149
status: 409,
150150
statusText: 'Conflict',

test/e2e/v3.axios.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('v3.axios', () => {
127127
expect(error).toBe(
128128
JSON.stringify({
129129
name: 'ApiError',
130-
message: 'Generic Error',
130+
message: 'Generic Error: status: 409; status text: Conflict; body: {\n "status": 409,\n "message": "hello world"\n}',
131131
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
132132
status: 409,
133133
statusText: 'Conflict',

0 commit comments

Comments
 (0)