Skip to content

Commit f212d87

Browse files
authored
Fix error handling for unauthorized responses (#479)
1 parent 7da024e commit f212d87

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

.bundlewatch.config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"//": "Pre-bundled for Browser (UMD)",
55
"path": "dist/browser/hibp.umd.js",
6-
"maxSize": "9.2 kB"
6+
"maxSize": "9.3 kB"
77
},
88
{
99
"//": "Pre-bundled for Browser (ESM)",

.changeset/shy-nails-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'hibp': patch
3+
---
4+
5+
Fix error handling for 401 Unauthorized API responses. The [haveibeenpwned.com API (v3)](https://haveibeenpwned.com/API/v3#Authorisation) changed its response type from a JSON body to text.

src/api/haveibeenpwned/__tests__/fetch-from-api.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@ describe('internal (haveibeenpwned): fetchFromApi', () => {
7979
it('throws an "Unauthorized" error', () => {
8080
server.use(
8181
http.get('*', () => {
82-
return new Response(JSON.stringify(UNAUTHORIZED.body), {
83-
status: UNAUTHORIZED.status,
84-
});
82+
return new Response(UNAUTHORIZED.text, { status: UNAUTHORIZED.status });
8583
}),
8684
);
8785

8886
return expect(fetchFromApi('/service/unauthorized')).rejects.toMatchInlineSnapshot(
89-
`[Error: Access denied due to missing hibp-api-key.]`,
87+
`[Error: Your request to the API couldn't be authorised. Check you have the right value in the "hibp-api-key" header, refer to the documentation for more: https://haveibeenpwned.com/API/v3#Authorisation]`,
9088
);
9189
});
9290
});

src/api/haveibeenpwned/fetch-from-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export async function fetchFromApi(
104104
throw new Error(BAD_REQUEST.statusText);
105105
}
106106
case UNAUTHORIZED.status: {
107-
const body = (await response.json()) as unknown as ErrorData;
108-
throw new Error(body.message);
107+
const message = await response.text();
108+
throw new Error(message);
109109
}
110110
case FORBIDDEN.status: {
111111
const rayId = response.headers.get('cf-ray');

src/api/haveibeenpwned/responses.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface HaveIBeenPwnedApiResponse {
1818
status: number;
1919
statusText?: string;
2020
body?: ResponseBody;
21+
text?: string;
2122
}
2223

2324
const emptyHeaders = new Map<string, string>();
@@ -30,19 +31,15 @@ export const BAD_REQUEST: HaveIBeenPwnedApiResponse = {
3031
};
3132

3233
/**
33-
* This response has unique behavior. For some reason, the API includes an
34-
* object in the response body for this one, containing a human-readable
35-
* message. Manually populating the message here purely for use in tests.
34+
* The API includes a human-readable error message as text in the body of this
35+
* response type. Manually populating the message here purely for use in tests.
3636
*
3737
* @internal
3838
*/
3939
export const UNAUTHORIZED: HaveIBeenPwnedApiResponse = {
4040
headers: emptyHeaders,
4141
status: 401,
42-
body: {
43-
statusCode: 401,
44-
message: 'Access denied due to missing hibp-api-key.',
45-
},
42+
text: `Your request to the API couldn't be authorised. Check you have the right value in the "hibp-api-key" header, refer to the documentation for more: https://haveibeenpwned.com/API/v3#Authorisation`,
4643
};
4744

4845
/** @internal */
@@ -65,9 +62,9 @@ export const NOT_FOUND: HaveIBeenPwnedApiResponse = {
6562
};
6663

6764
/**
68-
* This response has unique behavior. For some reason, the API includes an
69-
* object in the response body for this one, containing a human-readable
70-
* message. Manually populating the message here purely for use in tests.
65+
* The API includes a JSON object containing a human-readable message in the
66+
* body of this response type. Manually populating the message here purely for
67+
* use in tests.
7168
*
7269
* @internal
7370
*/

0 commit comments

Comments
 (0)