Error Handling

Understanding and handling API errors effectively

Error Response Format

All API errors follow a consistent JSON format:

{
  "error": "error_type",
  "message": "Human-readable error description",
  "status": 400
}

HTTP Status Codes

Status Code Description Common Causes
400 Bad Request
  • Invalid image format
  • Missing required fields
  • Image too large/small
401 Unauthorized
  • Missing API key
  • Invalid API key
  • Expired API key
403 Forbidden
  • Insufficient permissions
  • Account suspended
429 Too Many Requests
  • Rate limit exceeded
  • Quota exceeded
500 Internal Server Error
  • Server-side error
  • Temporary service disruption

Camera Error Handling

Our camera.js library provides built-in error handling for common issues:

try {
    const camera = new VerifyCamera((photoData) => {
        if (photoData) {
            console.log('Photo captured successfully');
        }
    });
    
    await camera.start();
} catch (error) {
    switch (error.name) {
        case 'NotAllowedError':
            console.error('Camera permission denied');
            break;
        case 'NotFoundError':
            console.error('No camera available');
            break;
        case 'NotSupportedError':
            console.error('Camera API not supported');
            break;
        default:
            console.error('Camera error:', error);
    }
}

API Error Handling

When making direct API calls, handle errors appropriately:

async function verifyFaces(reference, selfie) {
  try {
    const formData = new FormData();
    formData.append('reference', reference);
    formData.append('selfie', selfie);

    const response = await fetch('https://api.example.com/api/v2/verify', {
      method: 'POST',
      headers: {
        'Authorization': 'Bearer YOUR_API_KEY'
      },
      body: formData
    });

    if (!response.ok) {
      const error = await response.json();
      switch (response.status) {
        case 400:
          // Handle validation errors
          if (error.message.includes('No face detected')) {
            console.error('No face found in image');
          } else if (error.message.includes('Multiple faces')) {
            console.error('Multiple faces detected');
          } else {
            console.error('Invalid request:', error.message);
          }
          break;
        case 401:
          console.error('Authentication failed:', error.message);
          break;
        case 429:
          console.error('Rate limit exceeded:', error.message);
          break;
        default:
          console.error('API error:', error.message);
      }
      throw new Error(error.message);
    }

    const result = await response.json();
    if (result.is_spoof) {
      console.error('Spoof detected in selfie');
      return;
    }

    return result;
  } catch (error) {
    console.error('Request failed:', error);
    throw error;
  }
}

Best Practices

  • Always implement proper error handling in your code
  • Log errors for debugging and monitoring
  • Implement exponential backoff for rate limit errors
  • Display user-friendly error messages in your UI
  • Monitor error rates to detect potential issues