cimplify

Error Handling

Result pattern, retryable errors, and checkout error codes.

TS
const result = await client.catalogue.getProducts()

if (!result.ok) {
  console.error(result.error.code)       // 'NETWORK_ERROR', 'NOT_FOUND', etc.
  console.error(result.error.message)    // human-readable message
  console.error(result.error.retryable)  // boolean
  console.error(result.error.suggestion) // optional fix hint
  console.error(result.error.docs_url)   // optional link to docs
  return
}

console.log(result.value.items)

Result Pattern

Every SDK method returns a discriminated union. Always check result.ok before accessing result.value.

TS
type Result<T> =
  | { ok: true; value: T }
  | { ok: false; error: CimplifyError }

type CimplifyError = {
  code: string
  message: string
  retryable: boolean
  suggestion?: string
  docs_url?: string
}

CimplifyError Fields

FieldTypeDescription
codestringMachine-readable error code
messagestringHuman-readable description
retryablebooleanWhether the operation can be retried
suggestionstring?Suggested fix for the developer
docs_urlstring?Link to relevant documentation

Retry Helper

Check retryable and use exponential backoff. Max 3 attempts is a good default.

TS
async function withRetry<T>(
  fn: () => Promise<Result<T>>,
  maxAttempts = 3,
): Promise<Result<T>> {
  let lastResult: Result<T>

  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    lastResult = await fn()
    if (lastResult.ok) return lastResult
    if (!lastResult.error.retryable) return lastResult

    // Exponential backoff: 1s, 2s, 4s
    await new Promise(r => setTimeout(r, 1000 * Math.pow(2, attempt)))
  }

  return lastResult!
}

// Usage
const result = await withRetry(() => client.catalogue.getProducts({ limit: 10 }))

Common Checkout Error Codes

CodeRecoverableDescription
ALREADY_PROCESSINGNoA checkout is already in progress for this cart
PAYMENT_FAILEDYesPayment provider declined the transaction
TIMEOUTYesCheckout timed out waiting for payment confirmation
AUTH_INCOMPLETEYesCustomer must complete authentication before checkout
AUTH_LOSTYesSession expired during checkout
CANCELLEDYesCheckout was aborted by the customer
INVALID_CARTNoCart is empty or invalid
NETWORK_ERRORYesConnection failure during checkout

See /reference/error-codes for the full list of error codes across all services.

Branching on Error Codes

TS
const result = await client.cart.addItem({ item_id: 'prod_xxxxx', quantity: 1 })

if (!result.ok) {
  switch (result.error.code) {
    case 'ITEM_UNAVAILABLE':
      showToast('This item is no longer available')
      break
    case 'OUT_OF_STOCK':
      showToast('This item is out of stock')
      break
    case 'NETWORK_ERROR':
      if (result.error.retryable) showToast('Connection issue. Try again.')
      break
    default:
      showToast(result.error.message)
  }
}