Skip to content

Commit

Permalink
Merge pull request #805 from MuckRock/allanlasser/issue803
Browse files Browse the repository at this point in the history
Catch JSON errors in API error response
  • Loading branch information
allanlasser authored Nov 1, 2024
2 parents 31201b4 + d4ed3f7 commit 07c2f49
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/lib/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ export async function getApiResponse<T, E = unknown>(
}

if (isErrorCode(resp.status)) {
response.error = {
status: resp.status,
message: resp.statusText,
errors: resp.json ? await resp.json() : null,
};
try {
response.error = {
status: resp.status,
message: resp.statusText,
errors: resp.json ? await resp.json() : null,
};
} catch (error) {
console.error(error);
// if we fail parsing the error's JSON,
// just return the status
response.error = {
status: resp.status,
message: resp.statusText,
errors: null,
};
}

return response;
}
Expand Down

0 comments on commit 07c2f49

Please sign in to comment.