Skip to content

Commit ba687ab

Browse files
authored
Error on failed CDP connection (#755)
* Exit on failed CDP connection * changeset * added custom error * remove template literal
1 parent e1f7074 commit ba687ab

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

.changeset/chatty-owls-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand-lib": patch
3+
---
4+
5+
Fix context init error on undefined context

lib/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
UnsupportedModelError,
4444
UnsupportedAISDKModelProviderError,
4545
InvalidAISDKModelFormatError,
46+
StagehandInitError,
4647
} from "../types/stagehandErrors";
4748
import { z } from "zod";
4849
import { GotoOptions } from "@/types/playwright";
@@ -754,6 +755,16 @@ export class Stagehand {
754755
});
755756
this.contextPath = contextPath;
756757
this._browser = browser;
758+
if (!context) {
759+
const errorMessage =
760+
"The browser context is undefined. This means the CDP connection to the browser failed";
761+
this.stagehandLogger.error(
762+
this.env === "LOCAL"
763+
? `${errorMessage}. If running locally, please check if the browser is running and the port is open.`
764+
: errorMessage,
765+
);
766+
throw new StagehandInitError(errorMessage);
767+
}
757768
this.stagehandContext = await StagehandContext.init(context, this);
758769

759770
const defaultPage = (await this.stagehandContext.getStagehandPages())[0];

types/stagehandErrors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,9 @@ ${JSON.stringify(issues, null, 2)}`);
183183
this.name = "ZodSchemaValidationError";
184184
}
185185
}
186+
187+
export class StagehandInitError extends StagehandError {
188+
constructor(message: string) {
189+
super(message);
190+
}
191+
}

0 commit comments

Comments
 (0)