Storefront API errors
The Storefront API client throws StorefrontApiError when a request fails and StorefrontTimeoutError when a request times out. Catch the errors to log the request ID for support and to show a fallback page.
Import StorefrontApiError and StorefrontTimeoutError from @shopify/hydrogen.
Anchor to Base errorBase error
Anchor to storefrontapierrorStorefrontApiError
Signals that a Storefront API request failed. The client throws the error for an HTTP error status, a network failure, or a response body that isn't a JSON object.
GraphQL errors, including THROTTLED, don't throw. Read GraphQL errors from the result's errors field.
When the request context's signal or the call's signal aborts before the call, the client throws the abort reason and sends no request. An during the request reaches your code unchanged. Other abort reasons during the request throw a .
- Anchor to requestIdrequest
Idrequest Id stringstringrequiredrequired The
x-request-idheader of the Storefront API response, when the response has one. Include the ID in support requests.- Anchor to statusstatusstatusnumbernumberrequiredrequired
The HTTP status code of the response, when the request reached the server.
- Anchor to queryTextquery
Textquery Text stringstringrequiredrequired The GraphQL query that failed. Hydrogen sets the value only in development builds.
- Anchor to variablesvariablesvariablesRecord<string, unknown>Record<string, unknown>requiredrequired
The variables that the client sent with the request. Hydrogen sets the value only in development builds.
- Anchor to namenamenamestringstringrequiredrequired
.sets its own class name.- Anchor to messagemessagemessagestringstringrequiredrequired
Describes the failure, such as the HTTP status that the API returned.
- Anchor to stackstackstackstringstringrequiredrequired
The stack trace, when the JavaScript runtime records one.
- Anchor to toJSONto
JSONto JSON () => { name: string; message: string; requestId?: string; status?: number; }() => { name: string; message: string; requestId?: string; status?: number; }requiredrequired Returns the error name, message, request ID, and status for logging. The output leaves out the query text, variables, cause, and stack.
- Anchor to __@toStringTag@19600_
_ @to String Tag@19600_ _ @to String Tag@19600 stringstringrequiredrequired Makes
report the error name, such as.
TypeScript
Examples
Description
Catch Storefront API request failures and return a fallback. This example returns `null` for GraphQL errors and for `StorefrontApiError`, and rethrows anything else.
TypeScript
import { gql, StorefrontApiError, type StorefrontClient } from "@shopify/hydrogen"; const SHOP_QUERY = gql(`query { shop { name } }`); export async function getShopName(storefront: StorefrontClient) { try { const { data, errors } = await storefront.graphql(SHOP_QUERY); if (errors) return null; return data.shop.name; } catch (error) { if (error instanceof StorefrontApiError) { console.error("Storefront API request failed", error.status, error.requestId); return null; } throw error; } }
Anchor to Timeout errorTimeout error
Anchor to storefronttimeouterrorStorefrontTimeoutError
Signals that a Storefront API request ran longer than the client's setting. Aborts from the request context or the call's signal throw other errors.
extends . Check for first when you handle both errors.
- Anchor to timeoutInMstimeout
In Mstimeout In Ms numbernumberrequiredrequired The timeout that the request exceeded, in milliseconds.
- Anchor to namenamenamestringstringrequiredrequired
Always
.- Anchor to messagemessagemessagestringstringrequiredrequired
States the timeout, such as
"Storefront API request timed out after 30000ms".- Anchor to stackstackstackstringstringrequiredrequired
The stack trace, when the JavaScript runtime records one.
- Anchor to requestIdrequest
Idrequest Id stringstringrequiredrequired The
x-request-idheader of the Storefront API response, when the response has one. Include the ID in support requests.- Anchor to statusstatusstatusnumbernumberrequiredrequired
The HTTP status code of the response, when the request reached the server.
- Anchor to queryTextquery
Textquery Text stringstringrequiredrequired The GraphQL query that failed. Hydrogen sets the value only in development builds.
- Anchor to variablesvariablesvariablesRecord<string, unknown>Record<string, unknown>requiredrequired
The variables that the client sent with the request. Hydrogen sets the value only in development builds.
- Anchor to toJSONto
JSONto JSON () => { name: string; message: string; requestId?: string; status?: number; }() => { name: string; message: string; requestId?: string; status?: number; }requiredrequired Returns the error name, message, request ID, and status for logging. The output leaves out the query text, variables, cause, and stack.
- Anchor to __@toStringTag@19600_
_ @to String Tag@19600_ _ @to String Tag@19600 stringstringrequiredrequired Makes
report the error name, such as.
TypeScript
Examples
Description
Set a 5-second timeout on the client and catch timeouts. This example checks `StorefrontTimeoutError` before `StorefrontApiError`.
TypeScript
import { createShopifyRequestContext, createStorefrontClient, gql, StorefrontApiError, StorefrontTimeoutError, } from "@shopify/hydrogen"; const storefront = createStorefrontClient({ type: "public", requestContext: createShopifyRequestContext({ request: { headers: new Headers() }, i18n: { country: "US", language: "EN" }, }), config: { storeDomain: "my-store.myshopify.com", defaultTimeoutInMs: 5_000 }, }); try { await storefront.graphql(gql(`query { shop { name } }`)); } catch (error) { if (error instanceof StorefrontTimeoutError) { console.warn(`Timed out after ${error.timeoutInMs}ms`); } else if (error instanceof StorefrontApiError) { console.error(error.message); } }