Error Handling
Silent mode (default)​
By default, the SDK operates in silent mode. If a ping fails — due to a network error, timeout, or API error — it is logged as a console.warn and your job continues normally.
This is the correct behaviour for production. A Crontify outage should never take down your scheduled tasks.
const monitor = new CrontifyMonitor({
apiKey: process.env.CRONTIFY_API_KEY!,
monitorId: process.env.CRONTIFY_MONITOR_ID!,
silent: true, // default
});
// If this ping fails, your job still runs
await monitor.start();
await doWork();
await monitor.success();
Non-silent mode​
Set silent: false if you want ping failures to throw — useful in tests or when you want strict failure behaviour:
const monitor = new CrontifyMonitor({
apiKey: process.env.CRONTIFY_API_KEY!,
monitorId: process.env.CRONTIFY_MONITOR_ID!,
silent: false,
});
Error types​
When silent: false, you can catch specific error types:
import {
CrontifyApiError,
CrontifyTimeoutError,
CrontifyNetworkError,
} from '@crontify/sdk';
try {
await monitor.start();
} catch (error) {
if (error instanceof CrontifyApiError) {
// HTTP 4xx or 5xx from the API
console.error(`API error ${error.statusCode}: ${error.message}`);
} else if (error instanceof CrontifyTimeoutError) {
// Request timed out (default: 10,000ms)
console.error('Crontify ping timed out');
} else if (error instanceof CrontifyNetworkError) {
// Could not reach the API
console.error('Could not connect to Crontify API');
}
}
Job errors vs ping errors​
These are two separate concerns:
| Error type | What it means | What to do |
|---|---|---|
| Job error | Your job logic threw | Call monitor.fail({ message: err.message }) |
| Ping error | Crontify API was unreachable | In silent mode, nothing — the job continues |
wrap() handles this correctly. If your job throws, wrap() calls monitor.fail() then re-throws the original error. If fail() itself throws (ping error in non-silent mode), the original error is still re-thrown.
Timeout configuration​
The default request timeout is 10 seconds. Adjust it if your network is slow:
const monitor = new CrontifyMonitor({
apiKey: process.env.CRONTIFY_API_KEY!,
monitorId: process.env.CRONTIFY_MONITOR_ID!,
timeout: 5000, // 5 seconds
});