Error messages should include specific context about what went wrong and provide actionable guidance for resolution. Avoid generic error messages that leave developers guessing about the root cause or solution.
When throwing errors:
Example of improved error messaging:
// Poor: Generic error message
throw new Error("act() is not implemented on the base page object");
// Better: Contextual error with actionable guidance
throw new Error(
"act() is not implemented on the base page object. Ensure you are calling init() on the Stagehand object before calling methods on the page object."
);
// Best: Include original error details when wrapping
constructor(error?: unknown) {
if (error instanceof Error || error instanceof StagehandError) {
super(
`\nHey! We're sorry you ran into an error. \nIf you need help, please open a Github issue or reach out to us on Slack: https://stagehand.dev/slack\n\nFull error:\n${error.message}`
);
}
}
This approach reduces debugging time and improves the developer experience by making failures self-explanatory.
Enter the URL of a public GitHub repository