Skip to content

Commit

Permalink
Explicitly check for response ok false in codeServerRequest (#2383)
Browse files Browse the repository at this point in the history
* Explicitly check for response ok false in codeServerRequest

* Add unit test
  • Loading branch information
laurakwhit authored Oct 11, 2024
1 parent 241a40b commit bcc9f02
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/lib/services/data-encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@ describe('Codec Server Requests for Decode and Encode', () => {
codeServerRequest({ type: 'encode', payloads, namespace, settings }),
).rejects.toThrow();
});

it('should throw an error for encode if response is not ok', async () => {
const response = new Response(JSON.stringify(payloads), {
status: 500,
statusText: 'Internal Server Error',
});
global.fetch = vi.fn(() => Promise.resolve(response));

await expect(
codeServerRequest({ type: 'encode', payloads, namespace, settings }),
).rejects.toThrow();
});
});
3 changes: 1 addition & 2 deletions src/lib/services/data-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
getCodecIncludeCredentials,
getCodecPassAccessToken,
} from '$lib/utilities/get-codec';
import { has } from '$lib/utilities/has';
import { validateHttps } from '$lib/utilities/is-http';
import { stringifyWithBigInt } from '$lib/utilities/parse-with-big-int';

Expand Down Expand Up @@ -71,7 +70,7 @@ export async function codeServerRequest({
requestOptions,
)
.then((response) => {
if (has(response, 'ok') && !response.ok) {
if (response.ok === false) {
throw {
statusCode: response.status,
statusText: response.statusText,
Expand Down

0 comments on commit bcc9f02

Please sign in to comment.