8 min read

Error Handling

Learn how to handle API errors gracefully and implement retry logic.

Error Response Format

All error responses from the Pipe Solar API follow a consistent format:

{
  "error": {
    "code": "invalid_request",
    "message": "The 'capacity_kw' field must be a positive number.",
    "param": "capacity_kw",
    "status": 422
  }
}

HTTP Status Codes

CodeMeaningAction
400Bad RequestCheck request body/params
401UnauthorizedCheck API key
404Not FoundVerify resource ID
422Validation ErrorFix input data
429Rate LimitedWait and retry with backoff
500Server ErrorRetry; contact support if persists

Retry Strategy

For 429 and 5xx errors, implement exponential backoff:

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

    if (response.ok) return response.json();

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

    // Non-retryable error
    throw new Error(`API error: ${response.status}`);
  }
  throw new Error("Max retries exceeded");
}