Skip to main content

Error Handling (SDK)

The SDK throws typed error classes for different failure scenarios.

Error Classes​

ClassWhen
TanqoryAPIErrorAny non-2xx API response (400, 404, 422, 500, etc.)
TanqoryRateLimitError429 Too Many Requests (extends TanqoryAPIError)
TanqoryNetworkErrorNetwork failure, DNS, timeout
TanqoryAuthError401 after token refresh failed (extends TanqoryAPIError)

Basic Error Handling​

import {
TanqoryAPIError,
TanqoryRateLimitError,
TanqoryNetworkError,
TanqoryAuthError,
} from '@tanqory/sdk'

try {
const product = await tanqory.products.create(storeId, data)
} catch (error) {
if (error instanceof TanqoryAuthError) {
// Token expired and refresh failed — redirect to login
redirectToLogin()
} else if (error instanceof TanqoryRateLimitError) {
console.log(`Rate limited. Retry after ${error.retryAfter}s`)
console.log(`Limit: ${error.limit}`)
console.log(`Reset: ${error.resetAt}`) // Date object
} else if (error instanceof TanqoryAPIError) {
console.log(error.statusCode) // 400, 404, 422, etc.
console.log(error.code) // 'VALIDATION_ERROR', 'RESOURCE_NOT_FOUND', etc.
console.log(error.message) // 'Product title is required'
console.log(error.details) // Field-level errors (if validation)
} else if (error instanceof TanqoryNetworkError) {
console.log('Network error:', error.message)
}
}

TanqoryAPIError Properties​

PropertyTypeDescription
statusCodenumberHTTP status code
messagestringHuman-readable error message
codestringMachine-readable error code
detailsarrayField-level validation errors (optional)
requestIdstringUnique request ID for support

TanqoryRateLimitError Properties​

Extends TanqoryAPIError with:

PropertyTypeDescription
retryAfternumberSeconds to wait before retrying
limitnumberMax requests per window
remainingnumberRequests remaining (always 0)
resetAtDateWhen the rate limit window resets

Auto-Retry​

The SDK automatically retries on 429 and 5xx errors with exponential backoff:

const tanqory = createClient({
apiKey: 'pk_live_...',
maxRetries: 3, // Default: 3. Set to 0 to disable.
})

Retry behavior:

  • 429: Waits for Retry-After header value, then retries.
  • 500, 502, 503, 504: Retries with exponential backoff (1s, 2s, 4s).
  • 400, 401, 403, 404, 422: Not retried (client errors).

Disable Auto-Retry​

const tanqory = createClient({
apiKey: 'pk_live_...',
maxRetries: 0, // All errors throw immediately
})

Validation Error Details​

When the API returns 422, the details array contains field-level errors:

try {
await tanqory.products.create(storeId, { price: -100 })
} catch (error) {
if (error instanceof TanqoryAPIError && error.details) {
for (const detail of error.details) {
console.log(`${detail.field}: ${detail.message}`)
// "title: Product title is required"
// "price: Price must be a positive number"
}
}
}

React Error Handling​

With hooks, errors are available on the query/mutation result:

import { useProducts } from '@tanqory/sdk/react'
import { TanqoryAPIError } from '@tanqory/sdk'

function ProductList({ storeId }: { storeId: string }) {
const { data, error } = useProducts(storeId)

if (error instanceof TanqoryAPIError) {
return <div>API Error: {error.message} ({error.code})</div>
}

return <div>{data?.data.map(p => <div key={p.id}>{p.title}</div>)}</div>
}