Skip to content

Commit 78f1d29

Browse files
Merge pull request #1783 from maxmind/kevin/http-status
Add http status code to WebServiceClientError
2 parents 04c0430 + 038d191 commit 78f1d29

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ CHANGELOG
2323
network_error, http_error, parked, pre_development).
2424
* `lastVisitedOn` - The date the automated visit was completed.
2525
* `hasRedirect` - Whether the domain redirects to another URL.
26+
* Added the `status` property to `WebServiceClientError`. This property
27+
contains the HTTP status code when the error is the result of an HTTP
28+
response error. Network errors (timeouts, fetch errors) will not have a
29+
status code.
2630

2731
8.1.0 (2025-05-23)
2832
------------------

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface WebServiceClientError {
22
code: string;
33
error: string;
4+
status?: number;
45
url: string;
56
}

src/webServiceClient.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ describe('WebServiceClient', () => {
939939
return expect(client.score(transaction)).rejects.toEqual({
940940
code: 'SERVER_ERROR',
941941
error: 'Received a server error with HTTP status code: 500',
942+
status: 500,
942943
url: baseUrl + fullPath('score'),
943944
});
944945
});
@@ -954,6 +955,7 @@ describe('WebServiceClient', () => {
954955
return expect(client.score(transaction)).rejects.toEqual({
955956
code: 'HTTP_STATUS_CODE_ERROR',
956957
error: 'Received an unexpected HTTP status code: 300',
958+
status: 300,
957959
url: baseUrl + fullPath('score'),
958960
});
959961
});
@@ -969,6 +971,7 @@ describe('WebServiceClient', () => {
969971
return expect(client.score(transaction)).rejects.toEqual({
970972
code: 'INVALID_RESPONSE_BODY',
971973
error: 'Received an invalid or unparseable response body',
974+
status: 401,
972975
url: baseUrl + fullPath('score'),
973976
});
974977
});
@@ -1013,6 +1016,7 @@ describe('WebServiceClient', () => {
10131016
return expect(client.score(transaction)).rejects.toEqual({
10141017
code,
10151018
error,
1019+
status,
10161020
url: baseUrl + fullPath('score'),
10171021
});
10181022
});

src/webServiceClient.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,22 @@ export default class WebServiceClient {
150150
response: Response,
151151
url: string
152152
): Promise<WebServiceClientError> {
153-
if (response.status && response.status >= 500 && response.status < 600) {
153+
const status = response.status;
154+
155+
if (status && status >= 500 && status < 600) {
154156
return {
155157
code: 'SERVER_ERROR',
156-
error: `Received a server error with HTTP status code: ${response.status}`,
158+
error: `Received a server error with HTTP status code: ${status}`,
159+
status,
157160
url,
158161
};
159162
}
160163

161-
if (response.status && (response.status < 400 || response.status >= 600)) {
164+
if (status && (status < 400 || status >= 600)) {
162165
return {
163166
code: 'HTTP_STATUS_CODE_ERROR',
164-
error: `Received an unexpected HTTP status code: ${response.status}`,
167+
error: `Received an unexpected HTTP status code: ${status}`,
168+
status,
165169
url,
166170
};
167171
}
@@ -171,12 +175,12 @@ export default class WebServiceClient {
171175
data = (await response.json()) as ResponseError;
172176

173177
if (!data.code || !data.error) {
174-
return { ...invalidResponseBody, url };
178+
return { ...invalidResponseBody, status, url };
175179
}
176180
} catch {
177-
return { ...invalidResponseBody, url };
181+
return { ...invalidResponseBody, status, url };
178182
}
179183

180-
return { ...data, url } as WebServiceClientError;
184+
return { ...data, status, url } as WebServiceClientError;
181185
}
182186
}

0 commit comments

Comments
 (0)