The crawler.dev API uses standard HTTP status codes and returns detailed error information to help you build robust applications. This guide covers all error scenarios and how to handle them effectively.
HTTP Status Codes
Our API returns standard HTTP status codes to indicate the success or failure of requests:
Success Codes (2xx)
Code
Status
Description
200
OK
Request successful
201
Created
Resource created successfully
Client Error Codes (4xx)
Code
Status
Description
400
Bad Request
Invalid request format or missing required fields
401
Unauthorized
Invalid or missing API key
403
Forbidden
API key doesn't have required permissions
404
Not Found
Resource not found
413
Payload Too Large
File size exceeds maximum limit
415
Unsupported Media Type
File type not supported
422
Unprocessable Entity
Valid request but unable to process
429
Too Many Requests
Rate limit exceeded
Server Error Codes (5xx)
Code
Status
Description
500
Internal Server Error
Unexpected server error
502
Bad Gateway
Upstream service error
503
Service Unavailable
Service temporarily unavailable
504
Gateway Timeout
Request timeout
Error Response Format
All error responses follow a consistent JSON format defined in our OpenAPI specification:
{ "error": { "code": "INVALID_URL", "message": "The provided URL is not valid", "timestamp": "2024-01-01T12:00:00Z" }}
Unsupported URL Scheme
Code
{ "error": { "code": "UNSUPPORTED_SCHEME", "message": "URL scheme not supported. Only HTTP and HTTPS are allowed", "timestamp": "2024-01-01T12:00:00Z" }}
async function handleRateLimit(response) { const retryAfter = response.headers.get('Retry-After'); if (retryAfter) { const waitTime = parseInt(retryAfter) * 1000; // Convert to milliseconds console.log(`Rate limited. Waiting ${waitTime}ms before retry...`); await sleep(waitTime); } else { // Default backoff if no Retry-After header await sleep(60000); // Wait 1 minute }}
4. Validate Input Before API Calls
Validate files and URLs before sending to the API:
Code
function validateFile(file) { const maxSize = 50 * 1024 * 1024; // 50MB const supportedTypes = [ 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/plain' ]; if (file.size > maxSize) { throw new Error(`File size (${file.size} bytes) exceeds maximum of ${maxSize} bytes`); } if (!supportedTypes.includes(file.type)) { throw new Error(`File type ${file.type} is not supported`); } if (file.size === 0) { throw new Error('File cannot be empty'); }}function validateURL(url) { try { new URL(url); } catch { throw new Error('Invalid URL format'); } if (!url.startsWith('http://') && !url.startsWith('https://')) { throw new Error('URL must use HTTP or HTTPS protocol'); } // Check for common SSRF patterns const blockedPatterns = [ /^https?:\/\/localhost/, /^https?:\/\/127\.0\.0\.1/, /^https?:\/\/10\./, /^https?:\/\/172\.(1[6-9]|2[0-9]|3[0-1])\./, /^https?:\/\/192\.168\./ ]; for (const pattern of blockedPatterns) { if (pattern.test(url)) { throw new Error('URL blocked for security reasons'); } }}
5. Graceful Error Messages
Show user-friendly error messages based on standardized error codes:
Code
function getUserFriendlyErrorMessage(error) { const errorMessages = { 'MISSING_API_KEY': 'Authentication required. Please check your API key.', 'INVALID_API_KEY': 'Invalid API key. Please verify your credentials.', 'PAYLOAD_TOO_LARGE': 'File is too large. Maximum size is 50MB.', 'UNSUPPORTED_CONTENT_TYPE': 'File type not supported. Please use PDF, DOC, DOCX, or TXT files.', 'EMPTY_BUFFER': 'File cannot be empty. Please select a file with content.', 'QUOTA_EXHAUSTED': 'Too many requests. Please wait before trying again.', 'INVALID_URL': 'Invalid URL format. Please check the URL and try again.', 'UNSUPPORTED_SCHEME': 'URL must use HTTP or HTTPS protocol.', 'SSRF_BLOCKED': 'URL blocked for security reasons. Please use a public URL.', 'FETCH_FAILED': 'Unable to access the provided URL. Please check the URL and try again.', 'CONTENT_TOO_LARGE': 'Content is too large to process. Please try a smaller file or URL.', 'EXTRACTOR_TIMEOUT': 'Processing timed out. Please try again with a smaller file.', 'EXTRACTOR_ERROR': 'Failed to extract text. Please try a different file or URL.', 'VALIDATION_ERROR': 'Invalid request. Please check your input and try again.', 'NOT_FOUND': 'Resource not found. Please check the URL or endpoint.', 'INTERNAL': 'Service temporarily unavailable. Please try again later.' }; return errorMessages[error.error.code] || error.error.message || 'An unexpected error occurred.';}
Implement appropriate recovery strategies for different error codes:
Code
async function handleAPIError(error, originalRequest) { switch (error.error.code) { case 'MISSING_API_KEY': case 'INVALID_API_KEY': // Refresh token or redirect to login await refreshAuthToken(); return retryRequest(originalRequest); case 'QUOTA_EXHAUSTED': // Wait and retry await handleRateLimit(error); return retryRequest(originalRequest); case 'EXTRACTOR_ERROR': case 'UNSUPPORTED_CONTENT_TYPE': case 'EMPTY_BUFFER': // Don't retry, show user error throw new UserError(getUserFriendlyErrorMessage(error)); case 'INTERNAL': case 'EXTRACTOR_TIMEOUT': // Retry with backoff return retryWithBackoff(originalRequest); default: throw error; }}
Testing Error Scenarios
Test your error handling with these scenarios:
Invalid API key: Use a fake API key to test INVALID_API_KEY errors
Large files: Test with files over 50MB to trigger PAYLOAD_TOO_LARGE errors
Empty files: Test with empty files to trigger EMPTY_BUFFER errors
Unsupported files: Test with image files to trigger UNSUPPORTED_CONTENT_TYPE errors
Invalid URLs: Test with malformed URLs to trigger INVALID_URL errors
Blocked URLs: Test with localhost URLs to trigger SSRF_BLOCKED errors
Network issues: Simulate network timeouts to trigger FETCH_FAILED errors
Error Code Reference
For quick reference, here are all the standardized error codes organized by category:
Authentication & Authorization
MISSING_API_KEY - No API key provided
INVALID_API_KEY - Invalid or disabled API key
File Processing
NO_FILE - No file in request
EMPTY_BUFFER - Empty file content
UNSUPPORTED_CONTENT_TYPE - Unsupported file format
PAYLOAD_TOO_LARGE - File too large
URL Processing
INVALID_URL - Malformed URL
UNSUPPORTED_SCHEME - Non-HTTP/HTTPS URL
SSRF_BLOCKED - Security-blocked URL
FETCH_FAILED - Failed to fetch content
CONTENT_TOO_LARGE - Content too large
Processing
EXTRACTOR_TIMEOUT - Text extraction timeout
EXTRACTOR_ERROR - Text extraction failure
UNSUPPORTED_CONTENT_TYPE - Unsupported content format
System
QUOTA_EXHAUSTED - Rate limit exceeded
INTERNAL - Internal server error
VALIDATION_ERROR - Request validation failed
NOT_FOUND - Resource not found
TIMEOUT - General request timeout
Remember: Robust error handling using these standardized error codes is crucial for a good user experience and helps with debugging issues in production.