Skip to content

Commit

Permalink
Improve error message when storefront API is not JSON (#1444)
Browse files Browse the repository at this point in the history
* Propagate a better error message when the response from the storefront API is not JSON parseable
  • Loading branch information
blittle authored Jun 7, 2022
1 parent c7dc644 commit 0b4ee48
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-years-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen': patch
---

Propagate a better error message when the response from the storefront API is not JSON parseable
11 changes: 9 additions & 2 deletions packages/hydrogen/src/hooks/useShopQuery/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,23 @@ export function useShopQuery<T>({
const body = query ? graphqlRequestBody(query, variables) : '';
const {url, requestInit} = useCreateShopRequest(body); // eslint-disable-line react-hooks/rules-of-hooks

let text: string;
let data: any;
let useQueryError: any;

try {
data = fetchSync(url, {
text = fetchSync(url, {
...requestInit,
cache,
preload,
shouldCacheResponse,
}).json();
}).text();

try {
data = JSON.parse(text);
} catch (error: any) {
useQueryError = new Error('Unable to parse response:\n' + text);
}
} catch (error: any) {
// Pass-through thrown promise for Suspense functionality
if (error?.then) {
Expand Down

0 comments on commit 0b4ee48

Please sign in to comment.