-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handling.ts
More file actions
41 lines (39 loc) · 1.21 KB
/
error-handling.ts
File metadata and controls
41 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Error handling — catch and handle SDK errors gracefully.
*
* The SDK throws specific error classes:
* - ForgeBinaryNotFoundError — forge binary not found
* - ForgeProcessError — forge exited with non-zero code
* - ForgeOutputParseError — JSON extraction failed (internal, not thrown to caller)
* - ForgeAbortError — query was cancelled
*
* Run: bun run examples/error-handling.ts
*/
import { ForgeBinaryNotFoundError, query, resolveForgePath } from "../src";
try {
// Try to resolve the forge binary
const forgePath = resolveForgePath();
console.log(`Forge binary found at: ${forgePath}`);
// Run a query
for await (const message of query({
prompt: "Say hello in exactly one word.",
})) {
switch (message.type) {
case "result":
console.log(`Result: ${message.result}`);
break;
case "error":
console.error(`Forge error: ${message.error}`);
break;
}
}
} catch (err) {
if (err instanceof ForgeBinaryNotFoundError) {
console.error("Forge binary not found!");
console.error("Install forge or set FORGE_PATH environment variable.");
process.exit(1);
} else {
console.error("Unexpected error:", err);
process.exit(1);
}
}