Error Handling
The SDK throws ScrayleError for all API and connection errors.
ScrayleError
import { ScrayleError } from '@scrayle/sdk';
try {
const result = await client.scrapingCrawl.scrapeUrl('https://example.com');
} catch (error) {
if (error instanceof ScrayleError) {
console.log(error.message); // Human-readable message
console.log(error.statusCode); // HTTP status code
console.log(error.response); // Raw response body
}
}Common Error Codes
| Status | Meaning | Action |
|---|---|---|
| 401 | Invalid or missing API key | Check your SCRAYLE_API_KEY |
| 402 | Insufficient credits | Add credits in the dashboard |
| 403 | Access denied | Check permissions for the resource |
| 404 | Resource not found | Verify the ID or path exists |
| 429 | Rate limited | Slow down requests, use retry with backoff |
| 500 | Server error | Retry the request |
| 502/503 | Service unavailable | Retry after a delay |
Retry Pattern
import { ScrayleClient, ScrayleError, retry } from '@scrayle/sdk';
const client = new ScrayleClient();
const result = await retry(
async () => {
return await client.scrapingCrawl.scrapeUrl('https://example.com');
},
{ retries: 3, minTimeout: 2000 }
);Browser Connection Errors
Browser sessions may disconnect due to TTL expiry or network issues:
import { chromium } from 'playwright-core';
const { browserWSEndpoint } = await client.browser.create({
sessionTTL: 300,
});
const browser = await chromium.connectOverCDP(browserWSEndpoint);
browser.on('disconnected', () => {
console.log('Browser session ended');
// Reconnect or handle gracefully
});Timeout Handling
const client = new ScrayleClient({
timeout: 120000, // 2 minutes for all requests
});
// Or handle timeouts per-call
try {
const result = await client.scrapingCrawl.crawlUrl(
'https://large-site.com',
{ limit: 1000 }
);
} catch (error) {
if (error.message.includes('timeout')) {
// Use async crawl for large sites
const { id } = await client.scrapingCrawl.crawl.asyncCrawlUrl(
'https://large-site.com',
{ limit: 1000 }
);
}
}