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 Handling for Non-200 Status Codes in shopifyFetch #1191

Open
rondo10 opened this issue Sep 6, 2023 · 0 comments
Open

Improve Error Handling for Non-200 Status Codes in shopifyFetch #1191

rondo10 opened this issue Sep 6, 2023 · 0 comments

Comments

@rondo10
Copy link

rondo10 commented Sep 6, 2023

Issue Description:

When interacting with Shopify's GraphQL API, sometimes the response may not have a 200 status code (e.g., 403 Forbidden). In such cases, the current implementation of shopifyFetch doesn't handle the error gracefully. Specifically, it attempts to parse the JSON response, leading to a 'Unexpected end of JSON input' error.

Current Behavior:

When a non-200 status code is returned by Shopify, the following error is thrown:

{
  "cause": "unknown",
  "status": 500,
  "message": "Unexpected end of JSON input"
}

Expected Behavior:

The function should throw a meaningful ShopifyErrorLike object, with a relevant status code and a detailed error message.

Suggested Fix:

One approach to solve this issue could be to check the response status before attempting to parse the JSON body. If the status code is not 200, a ShopifyErrorLike object could be thrown to allow for easier debugging and error handling.

if (result.status !== 200) {
  // NOTE: The `isShopifyError` method will return false on this type, I don't know why
  const error: ShopifyErrorLike = {
    status: result.status,
    message: new Error(`HTTP status ${result.status}`),
    cause: new Error('Shopify request failed')
  };
  throw error;
}

Additional Context:

According to Shopify's GraphQL API documentation, a response may have a status code other than 200 even when using GraphQL. This makes it imperative for shopifyFetch to handle such scenarios appropriately.
"""
GraphQL HTTP status codes are different from REST API status codes. Most importantly, the GraphQL API can return a 200 OK response code in cases that would typically produce 4xx or 5xx errors in REST.
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant