shopify-api-js icon indicating copy to clipboard operation
shopify-api-js copied to clipboard

Storefront client not showing exact error instead showing invalid json

Open mehmehsloth opened this issue 2 years ago • 2 comments

Issue summary

I tried running storefront client to fetch products from my store. I intentionally put wrong storefront token to check the error message. Apparently I got invalid json input error. Here's the snippet code I am running ( example code provided in documentation ).

  const client = new Shopify.Clients.Storefront(
    shopifyShopName,
    storefrontToken,
  )
  try {
    const products = await client.query({
      data: {
        query: `query GetProducts($first: Int!, $query: String!) {
      products (first: $first, query: $query) {
        edges {
          node {
            id
            title
            descriptionHtml
          }
        }
      }
    }`,
        variables: {
          first: 1,
          query: 'a',
        },
      },
    })

    return products.body
  } catch (error) {
    console.log({ error })
    throw new Error(error.message)
  }

Expected behavior

Error should be provided by shopify api. In this particular case it should something like token is not valid.

Actual behavior

I was given error about invalid json response body.

error: HttpRequestError: Failed to make Shopify HTTP request: FetchError: invalid json response body at https://{my-shop-name}/api/unstable/graphql.json reason: Unex
pected end of JSON input
      at HttpRequestError.ShopifyError [as constructor] ({directory}/@shopify/shopify-api/dist/error.js:13:28)
      at new HttpRequestError ({directory}/@shopify/shopify-api/dist/error.js:79:42)
      at {directory}/@shopify/shopify-api/dist/clients/http_client/http_client.js:242:35
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
}

Steps to reproduce the problem

  1. Create new storefront client.
  2. Try any request that fails ( enter invalid token or shopname )

Checklist

  • [x] I have described this issue in a way that is actionable (if possible)

mehmehsloth avatar May 02 '22 10:05 mehmehsloth

Also getting this intermittently, from admin api: Failed to make Shopify HTTP request: FetchError: invalid json response body at https://shop.myshopify.com/admin/api/2022-04/graphql.json reason: Unexpected token < in JSON at position 0

without any other detail. Would help to log the actual response, probably it has some detail about the error.

mariusa avatar May 06 '22 12:05 mariusa

The same issue is happening in my server HttpRequestError: Failed to make Shopify HTTP request: FetchError: invalid json response body at https://wonderchefind.myshopify.com/admin/api/2022-07/graphql.json but with using the Admin API. Did someone found a solution for this?

oribenez avatar Sep 08 '22 07:09 oribenez

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Nov 08 '22 02:11 github-actions[bot]

The same issue is happening in my server HttpRequestError: Failed to make Shopify HTTP request: FetchError: invalid json response body at https://wonderchefind.myshopify.com/admin/api/2022-07/graphql.json but with using the Admin API. Did someone found a solution for this?

Hi sorry for replying this late. This issue can be fixed usually by generating new token. @oribenez

mehmehsloth avatar Nov 15 '22 11:11 mehmehsloth

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Jan 31 '23 02:01 github-actions[bot]

We are closing this issue because it has been inactive for a few months. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on since it was submitted, then it seems other issues have taken priority.

If you still encounter this issue with the latest stable version, please reopen using the issue template. You can also contribute directly by submitting a pull request– see the CONTRIBUTING.md file for guidelines

Thank you!

github-actions[bot] avatar Feb 14 '23 02:02 github-actions[bot]