Skip to content

Commit 1a4d0ad

Browse files
feat(error): handle non-json response from fetch request
1 parent 309b11f commit 1a4d0ad

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/error/index.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ export class IndiekitError extends Error {
4040
}
4141

4242
static async fromFetch(response) {
43-
const body = await response.json();
44-
const message = body.error_description || response.statusText;
43+
let body;
44+
let message = response.statusText;
45+
46+
try {
47+
// Parse JSON response, if provided
48+
body = await response.json();
49+
message = body.error_description || response.statusText;
50+
} catch (error) {
51+
console.error(error);
52+
}
53+
4554
return new IndiekitError(message, {
4655
status: response.status,
47-
code: body.error || response.statusText,
48-
cause: body.cause,
56+
code: body?.error || response.statusText,
57+
cause: body?.cause,
4958
});
5059
}
5160

0 commit comments

Comments
 (0)