Skip to content

Commit bc7bd66

Browse files
Document onSqlError hook for customizable SQL error logging
Add documentation for the new onSqlError hook that allows developers to customize SQL error logging behavior in Agents. The hook is called before errors are thrown via onError, enabling custom logging, monitoring, or observability integrations. Changes: - Added onSqlError method signature to Agent class API reference - Included practical example showing custom error logging with context - Documented that errors are still thrown via onError after this hook Related PR: cloudflare/agents#768 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
1 parent 537b3b8 commit bc7bd66

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/content/docs/agents/api-reference/agents-api.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class MyAgent extends Agent<Env, State> {
111111
console.error(`Connection error:`, error);
112112
}
113113

114+
// Handle SQL execution errors - override to customize logging behavior
115+
onSqlError(query: string, error: unknown): void {
116+
console.error(`failed to execute sql query: ${query}`, error);
117+
}
118+
114119
// Handle WebSocket connection close events
115120
async onClose(
116121
connection: Connection,
@@ -533,6 +538,11 @@ class Agent<Env, State = unknown> {
533538
strings: TemplateStringsArray,
534539
...values: (string | number | boolean | null)[]
535540
): T[];
541+
542+
// Handle SQL execution errors
543+
// Override this method to customize SQL error logging behavior
544+
// The error is still thrown via onError after this method is called
545+
onSqlError(query: string, error: unknown): void;
536546
}
537547
```
538548

@@ -585,6 +595,20 @@ export class YourAgent extends Agent {
585595
ORDER BY created_at DESC
586596
`;
587597
}
598+
599+
// Override onSqlError to customize SQL error handling
600+
onSqlError(query: string, error: unknown): void {
601+
// Custom logging that includes additional context
602+
console.error("SQL execution failed:", {
603+
query,
604+
error: error instanceof Error ? error.message : String(error),
605+
timestamp: new Date().toISOString(),
606+
agentId: this.name,
607+
});
608+
609+
// You could also send errors to an observability service
610+
// this.observability?.emit({ type: "sql_error", query, error }, this.ctx);
611+
}
588612
}
589613
```
590614

0 commit comments

Comments
 (0)