Skip to content

Error Handling

Learn how to handle errors from the Creed Space API.

Error Response Format

All errors follow a consistent format:

json
{
  "error": "error_code",
  "message": "Human-readable description",
  "details": { ... }
}

Error Codes

Authentication Errors (4xx)

CodeErrorDescription
401api_key_requiredNo API key provided
401invalid_api_keyKey is invalid or revoked
401expired_api_keyKey has expired
403insufficient_scopeKey lacks required permission
403ip_not_allowedRequest IP not in whitelist

Validation Errors (400)

ErrorDescription
validation_errorRequest body validation failed
invalid_jsonRequest body is not valid JSON
missing_fieldRequired field is missing

Rate Limiting (429)

ErrorDescription
rate_limit_exceededPer-minute limit exceeded
daily_limit_exceededPer-day limit exceeded

Server Errors (5xx)

CodeErrorDescription
500internal_errorUnexpected server error
503service_unavailableService temporarily unavailable

Handling Errors

Python

python
from creed_sdk import CreedSpace
from creed_sdk.exceptions import (
    CreedSpaceError,
    AuthenticationError,
    RateLimitError,
    ValidationError
)

client = CreedSpace(api_key="...")

try:
    result = client.safety.evaluate(text)
except AuthenticationError as e:
    # Invalid or missing API key
    logger.error(f"Auth failed: {e}")
    raise HTTPException(401, "API key error")

except RateLimitError as e:
    # Rate limited - wait and retry
    logger.warning(f"Rate limited, retry after {e.retry_after}s")
    time.sleep(e.retry_after)
    result = client.safety.evaluate(text)

except ValidationError as e:
    # Invalid request
    logger.error(f"Validation failed: {e.details}")
    raise HTTPException(400, "Invalid request")

except CreedSpaceError as e:
    # Other API errors
    logger.error(f"API error: {e}")
    raise HTTPException(500, "Safety check failed")

JavaScript

typescript
import {
    CreedSpace,
    CreedSpaceError,
    AuthenticationError,
    RateLimitError,
    ValidationError
} from '@creed-space/sdk';

const client = new CreedSpace({ apiKey: '...' });

try {
    const result = await client.safety.evaluate(text);
} catch (error) {
    if (error instanceof AuthenticationError) {
        console.error('Auth failed:', error.message);
        throw new Error('API key error');
    }

    if (error instanceof RateLimitError) {
        console.warn(`Rate limited, retry after ${error.retryAfter}s`);
        await sleep(error.retryAfter * 1000);
        return client.safety.evaluate(text);
    }

    if (error instanceof ValidationError) {
        console.error('Validation failed:', error.details);
        throw new Error('Invalid request');
    }

    if (error instanceof CreedSpaceError) {
        console.error('API error:', error.message);
        throw new Error('Safety check failed');
    }

    throw error;
}

Retry Strategies

Simple Retry

python
def evaluate_with_retry(text, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.safety.evaluate(text)
        except (RateLimitError, CreedSpaceError) as e:
            if attempt == max_retries - 1:
                raise
            time.sleep(1 * (attempt + 1))

Circuit Breaker

For production systems, use a circuit breaker pattern:

python
from circuitbreaker import circuit

@circuit(failure_threshold=5, recovery_timeout=30)
def evaluate_safe(text):
    return client.safety.evaluate(text)

Debugging

Enable Logging

python
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('creed_sdk')
logger.setLevel(logging.DEBUG)

Check Response Headers

python
try:
    result = client.safety.evaluate(text)
except CreedSpaceError as e:
    print(f"Request ID: {e.request_id}")
    print(f"Status: {e.status_code}")

Getting Help

If you encounter persistent errors:

  1. Check API Status
  2. Include the request ID when contacting support
  3. Review your Dashboard for usage issues

Constitutional AI for Safer Interactions