Error Handling
The Xec execution engine provides comprehensive error handling with a Result pattern, automatic retries, and detailed error context for robust command execution.
Overview
Error handling (packages/core/src/types/result.ts
) provides:
- Result pattern for explicit error handling
- Typed error codes for specific conditions
- Automatic retry logic with backoff
- Error context preservation across adapters
- Graceful degradation strategies
- Custom error handlers and recovery
Result Pattern
Basic Usage
import { $ } from '@xec-sh/core';
// Using nothrow() to get Result instead of throwing
const result = await $`command-that-might-fail`.nothrow();
if (result.ok) {
console.log('Success:', result.stdout);
} else {
console.error('Failed:', result.error.message);
console.error('Exit code:', result.exitCode);
console.error('Stderr:', result.stderr);
}
Result Type Definition
// Result type structure
interface ExecutionResult {
ok: boolean;
stdout: string;
stderr: string;
exitCode: number;
signal?: string;
error?: ExecutionError;
duration: number;
command: string;
}
// Error type structure
interface ExecutionError {
code: ErrorCode;
message: string;
cause?: Error;
context?: Record<string, any>;
}