Skip to content

Error Handling

The WalletHero API uses standard HTTP status codes and returns detailed error messages to help you debug issues.

Error Response Format

All errors follow this structure:

json
{
  "errors": [
    {
      "message": "Human-readable error description",
      "extensions": {
        "code": "ERROR_CODE",
        "field": "field_name"
      }
    }
  ]
}

For WalletHero extension endpoints, errors may use this simplified format:

json
{
  "error": "Error description",
  "reason": "Detailed reason"
}

HTTP Status Codes

CodeMeaningDescription
200OKRequest succeeded
201CreatedResource created successfully
204No ContentRequest succeeded with no response body
400Bad RequestInvalid request parameters
401UnauthorizedInvalid or missing authentication
403ForbiddenInsufficient permissions
404Not FoundResource doesn't exist
409ConflictResource already exists
422Unprocessable EntityValidation error
429Too Many RequestsRate limit exceeded
500Internal Server ErrorServer error

Common Error Codes

Authentication Errors

CodeDescription
INVALID_CREDENTIALSEmail or password is incorrect
INVALID_TOKENThe authentication token is invalid
TOKEN_EXPIREDThe session token has expired
EMAIL_NOT_VERIFIEDEmail address hasn't been verified

Validation Errors

CodeDescription
INVALID_PAYLOADRequest body is malformed or missing required fields
FAILED_VALIDATIONOne or more fields failed validation
RECORD_NOT_UNIQUEA record with the same unique field already exists

Permission Errors

CodeDescription
FORBIDDENYou don't have permission for this action
WORKSPACE_ACCESS_DENIEDYou don't have access to this workspace
INSUFFICIENT_ROLEYour role doesn't allow this action

Resource Errors

CodeDescription
RECORD_NOT_FOUNDThe requested resource doesn't exist
SEGMENT_NOT_FOUNDThe specified segment doesn't exist

Handling Errors in Code

SDK Error Handling

typescript
import { WalletHero, WalletHeroError } from '@wallethero/sdk';

const client = new WalletHero({ apiToken: 'your-token' });

try {
  const template = await client.passTemplates.get('non-existent-id');
} catch (error) {
  if (error instanceof WalletHeroError) {
    console.log('Status:', error.status);    // 404
    console.log('Code:', error.code);        // RECORD_NOT_FOUND
    console.log('Message:', error.message);  // Record not found
  }
}

REST API Error Handling

typescript
const response = await fetch('https://api.wallethero.app/items/pass_templates', {
  headers: { 'Authorization': `Bearer ${token}` }
});

if (!response.ok) {
  const error = await response.json();

  switch (response.status) {
    case 401:
      // Refresh token or re-authenticate
      break;
    case 403:
      // Check permissions
      break;
    case 429:
      // Wait and retry
      const retryAfter = response.headers.get('Retry-After');
      break;
    default:
      console.error('API Error:', error.errors?.[0]?.message);
  }
}

Validation Error Details

Validation errors include field-specific information:

json
{
  "errors": [
    {
      "message": "\"email\" must be a valid email",
      "extensions": {
        "code": "FAILED_VALIDATION",
        "field": "email"
      }
    },
    {
      "message": "\"password\" length must be at least 8 characters",
      "extensions": {
        "code": "FAILED_VALIDATION",
        "field": "password"
      }
    }
  ]
}

Retry Strategy

For transient errors (5xx, 429), implement exponential backoff:

typescript
async function fetchWithRetry(url: string, options: RequestInit, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    const response = await fetch(url, options);

    if (response.ok) return response;

    if (response.status >= 500 || response.status === 429) {
      const delay = Math.pow(2, i) * 1000; // 1s, 2s, 4s
      await new Promise(r => setTimeout(r, delay));
      continue;
    }

    throw new Error(`Request failed: ${response.status}`);
  }

  throw new Error('Max retries exceeded');
}

WalletHero Documentation