Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when storefront API is not JSON #1444

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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