Skip to content

Error Handling

The PaiTIENT Secure Model Service uses standard HTTP status codes and structured error responses to communicate API errors. This document details the error format and common error codes.

Error Response Format

All API errors are returned in a consistent JSON format:

json
{
  "error": {
    "code": "invalid_request",
    "message": "The request was invalid or malformed.",
    "param": "deployment_name",
    "details": "Deployment name must be between 3 and 64 characters.",
    "request_id": "req_abcdefghijk"
  }
}

The error object includes:

  • code: A machine-readable error code
  • message: A human-readable error message
  • param: The parameter that caused the error (if applicable)
  • details: Additional details about the error
  • request_id: A unique identifier for the request, useful for troubleshooting

HTTP Status Codes

Status CodeDescription
400Bad Request - The request was invalid or cannot be served
401Unauthorized - Authentication is required or failed
403Forbidden - The request is not allowed
404Not Found - The requested resource does not exist
409Conflict - The request conflicts with current state
422Unprocessable Entity - The request is well-formed but cannot be processed
429Too Many Requests - Rate limit exceeded
500Internal Server Error - Something went wrong on our end
503Service Unavailable - The service is temporarily unavailable

Common Error Codes

Authentication Errors

Error CodeDescription
invalid_api_keyThe API key is invalid
missing_api_keyNo API key was provided
expired_api_keyThe API key has expired
missing_client_idNo client ID was provided
invalid_client_idThe client ID is invalid

Request Errors

Error CodeDescription
invalid_requestThe request is malformed or invalid
invalid_parametersOne or more parameters are invalid
missing_required_parameterA required parameter is missing
parameter_type_errorA parameter is of the wrong type
invalid_jsonThe request body is not valid JSON

Resource Errors

Error CodeDescription
resource_not_foundThe requested resource does not exist
resource_existsThe resource already exists
resource_unavailableThe resource is temporarily unavailable
resource_lockedThe resource is locked and cannot be modified
deployment_failedThe deployment failed to create

Rate Limit Errors

Error CodeDescription
rate_limit_exceededYou have exceeded the rate limit
quota_exceededYou have exceeded your usage quota
concurrent_requests_limitToo many concurrent requests

Server Errors

Error CodeDescription
internal_errorAn internal error occurred
service_unavailableThe service is temporarily unavailable
model_unavailableThe requested model is temporarily unavailable
maintenance_in_progressThe service is undergoing maintenance

Handling Errors in SDK

Python SDK

The Python SDK raises exceptions for API errors:

python
from paitient_secure_model import Client
from paitient_secure_model.exceptions import (
    PaiTIENTApiError,
    AuthenticationError,
    ResourceNotFoundError,
    RateLimitError
)

client = Client()

try:
    deployment = client.get_deployment("non-existent-id")
except ResourceNotFoundError as e:
    print(f"Resource not found: {e.message}")
    print(f"Request ID for troubleshooting: {e.request_id}")
except AuthenticationError as e:
    print(f"Authentication failed: {e.message}")
except RateLimitError as e:
    print(f"Rate limit exceeded, retry after {e.retry_after} seconds")
except PaiTIENTApiError as e:
    print(f"API error: {e.code} - {e.message}")

JavaScript SDK

The JavaScript SDK uses a promise-based error handling approach:

javascript
const { PaiTIENTClient } = require('paitient-secure-model');
const { 
  PaiTIENTApiError, 
  AuthenticationError, 
  ResourceNotFoundError, 
  RateLimitError 
} = require('paitient-secure-model/errors');

const client = new PaiTIENTClient();

client.getDeployment('non-existent-id')
  .then(deployment => {
    console.log('Deployment:', deployment);
  })
  .catch(error => {
    if (error instanceof ResourceNotFoundError) {
      console.log(`Resource not found: ${error.message}`);
      console.log(`Request ID for troubleshooting: ${error.requestId}`);
    } else if (error instanceof AuthenticationError) {
      console.log(`Authentication failed: ${error.message}`);
    } else if (error instanceof RateLimitError) {
      console.log(`Rate limit exceeded, retry after ${error.retryAfter} seconds`);
    } else if (error instanceof PaiTIENTApiError) {
      console.log(`API error: ${error.code} - ${error.message}`);
    } else {
      console.log(`Unexpected error: ${error.message}`);
    }
  });

Retrying Failed Requests

For transient errors (such as 429 or 503), implement a retry mechanism with exponential backoff:

Python Example

python
import time
from paitient_secure_model import Client
from paitient_secure_model.exceptions import RateLimitError, ServiceUnavailableError

client = Client()

def with_retry(operation, max_retries=5, initial_backoff=1):
    retries = 0
    while True:
        try:
            return operation()
        except (RateLimitError, ServiceUnavailableError) as e:
            if retries >= max_retries:
                raise
            
            # Calculate backoff time (with jitter)
            backoff = initial_backoff * (2 ** retries) + (random.random() * 0.5)
            
            # Use retry-after header if available
            if hasattr(e, 'retry_after') and e.retry_after:
                backoff = e.retry_after
            
            print(f"Request failed, retrying in {backoff} seconds...")
            time.sleep(backoff)
            retries += 1

# Usage
deployment = with_retry(lambda: client.get_deployment("deployment-id"))

JavaScript Example

javascript
const { PaiTIENTClient } = require('paitient-secure-model');
const { RateLimitError, ServiceUnavailableError } = require('paitient-secure-model/errors');

const client = new PaiTIENTClient();

async function withRetry(operation, maxRetries = 5, initialBackoff = 1) {
  let retries = 0;
  while (true) {
    try {
      return await operation();
    } catch (error) {
      if (!(error instanceof RateLimitError || error instanceof ServiceUnavailableError) || 
          retries >= maxRetries) {
        throw error;
      }
      
      // Calculate backoff time (with jitter)
      let backoff = initialBackoff * (2 ** retries) + (Math.random() * 0.5);
      
      // Use retry-after header if available
      if (error.retryAfter) {
        backoff = error.retryAfter;
      }
      
      console.log(`Request failed, retrying in ${backoff} seconds...`);
      await new Promise(resolve => setTimeout(resolve, backoff * 1000));
      retries++;
    }
  }
}

// Usage
async function getDeployment() {
  const deployment = await withRetry(() => client.getDeployment('deployment-id'));
  console.log('Deployment:', deployment);
}

Validation Errors

For 422 Unprocessable Entity errors, the error response includes detailed validation errors:

json
{
  "error": {
    "code": "validation_error",
    "message": "The request contains invalid parameters",
    "request_id": "req_abcdefghijk",
    "validation_errors": [
      {
        "field": "min_replicas",
        "code": "less_than_minimum",
        "message": "Value must be at least 1"
      },
      {
        "field": "max_replicas",
        "code": "greater_than_maximum",
        "message": "Value must be at most 10"
      }
    ]
  }
}

Next Steps

Released under the MIT License.