Overview
Robust error handling is essential for production Stagehand applications. This guide covers error types, recovery strategies, and best practices.Error Types
Stagehand provides specific error classes for different failure modes.Timeout Errors
ActTimeoutError-act()operation timed outExtractTimeoutError-extract()operation timed outObserveTimeoutError-observe()operation timed outTimeoutError- Base timeout error class
Element Not Found Errors
Initialization Errors
Configuration Errors
LLM Response Errors
Validation Errors
Error Handling Patterns
Basic Try-Catch
Retry with Exponential Backoff
Graceful Degradation
Timeout Strategy
Handling Agent Errors
Agent Abort
Agent Max Steps Reached
Connection Errors
CDP Connection Closed
Browserbase Connection
Cleanup and Resource Management
Always Close
Detect Closed State
Logging Errors
Custom Logger
Persist Error Logs
Production Error Handling
Comprehensive Error Handler
Error Monitoring
Testing Error Scenarios
Error Prevention
Validate Before Acting
Wait for Page State
Use Appropriate Timeouts
Error Reference
Common errors fromsdkErrors.ts:1-438:
| Error Class | When It Occurs | Recovery Strategy |
|---|---|---|
ActTimeoutError | act() times out | Increase timeout, retry |
ExtractTimeoutError | extract() times out | Increase timeout, simplify extraction |
ObserveTimeoutError | observe() times out | Increase timeout, be more specific |
StagehandElementNotFoundError | Element doesn’t exist | Check page structure, wait for load |
StagehandNotInitializedError | Forgot to call init() | Call await stagehand.init() |
MissingLLMConfigurationError | No LLM API key | Set API key or provide LLM client |
ZodSchemaValidationError | Extraction doesn’t match schema | Adjust schema or prompt |
AgentAbortError | Agent can’t complete task | Adjust instruction or increase steps |
CdpConnectionClosedError | Browser connection lost | Create new Stagehand instance |
StagehandClosedError | Used after close() | Don’t use after closing |