Skip to content

Commit 1d22604

Browse files
custom error classes (#601)
* custom error classes * throw error when act, extract, observe are called instead of immediately * revert removal (should be done in separate PR) * rm dumb comments * changeset * more errors * global default error on init, act, extract, observe * minor * more errors
1 parent 066571f commit 1d22604

22 files changed

+836
-543
lines changed

Diff for: .changeset/four-hoops-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": minor
3+
---
4+
5+
add custom error classes

Diff for: evals/index.eval.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { EvalLogger } from "./logger";
3131
import { AvailableModel } from "@/dist";
3232
import { env } from "./env";
3333
import dotenv from "dotenv";
34+
import { StagehandEvalError } from "@/types/stagehandErrors";
3435
dotenv.config();
3536

3637
/**
@@ -226,8 +227,8 @@ const generateFilteredTestcases = (): Testcase[] => {
226227
const taskFunction = taskModule[input.name];
227228

228229
if (typeof taskFunction !== "function") {
229-
throw new Error(
230-
`Task function for ${input.name} is not a function`,
230+
throw new StagehandEvalError(
231+
`No Eval function found for task name: ${input.name}`,
231232
);
232233
}
233234

Diff for: evals/taskConfig.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import fs from "fs";
1414
import path from "path";
1515
import { AvailableModel, AvailableModelSchema } from "@/dist";
1616
import { filterByEvalName } from "./args";
17+
import { UnsupportedModelError } from "@/types/stagehandErrors";
1718

1819
// The configuration file `evals.config.json` contains a list of tasks and their associated categories.
1920
const configPath = path.join(__dirname, "evals.config.json");
@@ -62,7 +63,7 @@ const getModelList = (): string[] => {
6263
};
6364
const MODELS: AvailableModel[] = getModelList().map((model) => {
6465
if (!AvailableModelSchema.safeParse(model).success) {
65-
throw new Error(`Model ${model} is not a supported model`);
66+
throw new UnsupportedModelError(getModelList(), "Running evals");
6667
}
6768
return model as AvailableModel;
6869
});

Diff for: evals/tasks/peeler_simple.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EvalFunction } from "@/types/evals";
22
import { initStagehand } from "@/evals/initStagehand";
3+
import { StagehandEnvironmentError } from "@/types/stagehandErrors";
34

45
const env: "BROWSERBASE" | "LOCAL" =
56
process.env.EVAL_ENV?.toLowerCase() === "browserbase"
@@ -15,8 +16,10 @@ export const peeler_simple: EvalFunction = async ({ modelName, logger }) => {
1516
const { debugUrl, sessionUrl } = initResponse;
1617

1718
if (env === "BROWSERBASE") {
18-
throw new Error(
19-
"Browserbase not supported for this eval since we block all requests to file://",
19+
throw new StagehandEnvironmentError(
20+
"BROWSERBASE",
21+
"LOCAL",
22+
"peeler_simple eval",
2023
);
2124
}
2225

Diff for: examples/external_clients/ollama.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type {
2626
ChatCompletionUserMessageParam,
2727
} from "openai/resources/chat/completions";
2828
import { z } from "zod";
29+
import { CreateChatCompletionResponseError } from "@/types/stagehandErrors";
2930

3031
function validateZodSchema(schema: z.ZodTypeAny, data: unknown) {
3132
try {
@@ -226,7 +227,7 @@ export class OllamaClient extends LLMClient {
226227
if (options.response_model) {
227228
const extractedData = response.choices[0].message.content;
228229
if (!extractedData) {
229-
throw new Error("No content in response");
230+
throw new CreateChatCompletionResponseError("No content in response");
230231
}
231232
const parsedData = JSON.parse(extractedData);
232233

@@ -239,7 +240,7 @@ export class OllamaClient extends LLMClient {
239240
});
240241
}
241242

242-
throw new Error("Invalid response schema");
243+
throw new CreateChatCompletionResponseError("Invalid response schema");
243244
}
244245

245246
return parsedData;

0 commit comments

Comments
 (0)