feat(browser): Centralize and improve error handling in browser commands #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR significantly improves error handling within the browser commands (open, act, observe, extract), making it more robust, consistent, and developer-friendly. The key improvements are:
Centralized Error Types: A new file, src/commands/browser/stagehand/errors.ts, has been introduced to define all custom error classes used by the browser commands. This centralizes error management and improves code organization.
Consistent Use of Async Generators: Error handling now consistently uses async generators (CommandGenerator) to yield error messages. This ensures that all errors are properly propagated and displayed to the user, even in asynchronous operations.
Improved Error Message Formatting: Error messages are now more informative and include:
Unified Error Handling Logic: A handleBrowserError function is used to standardize error handling across all browser commands, ensuring consistent error reporting.
Duplicate Class Removal: Duplicate error class definitions have been removed. All browser commands now import and use the centralized error classes from errors.ts.
Improved act command error handling: The act command specifically benefits from better error reporting, including details about the failed instruction, start/end URLs, and available DOM elements when an ActionError occurs.
These changes significantly improve the developer experience when working with cursor-tools browser commands, making it easier to diagnose and fix issues.