Skip to content

Commit 51d1bfc

Browse files
Add documentation for onSqlError hook
Document the new onSqlError hook that allows users to customize SQL error logging behavior. This hook provides: - Ability to override the default console.error logging - Access to both the failed query and error details - Use cases for integrating with monitoring services 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 fb5b934 commit 51d1bfc

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ class MyAgent extends Agent<Env, State> {
127127
console.log("State updated:", state, "Source:", source);
128128
}
129129

130+
// Handle SQL execution errors. Override this method to customize logging behavior.
131+
onSqlError(query: string, error: unknown): void {
132+
console.error(`failed to execute sql query: ${query}`, error);
133+
}
134+
130135
// You can define your own custom methods to be called by requests,
131136
// WebSocket messages, or scheduled tasks
132137
async customProcessingMethod(data: any) {
@@ -596,6 +601,53 @@ Visit the [state management API documentation](/agents/api-reference/store-and-s
596601

597602
:::
598603

604+
### Error handling hooks
605+
606+
The Agent class provides hooks to customize error handling behavior.
607+
608+
#### onSqlError
609+
610+
Handle SQL execution errors. Override this method to customize logging behavior for SQL errors.
611+
612+
```ts
613+
class Agent<Env, State = unknown> {
614+
// Handle SQL execution errors
615+
// Override this method to customize logging behavior
616+
onSqlError(query: string, error: unknown): void;
617+
}
618+
```
619+
620+
<TypeScriptExample>
621+
622+
```ts
623+
// Example of customizing SQL error handling
624+
export class YourAgent extends Agent {
625+
// Override onSqlError to send errors to a monitoring service
626+
onSqlError(query: string, error: unknown): void {
627+
// Log to your monitoring service instead of console
628+
this.env.MONITORING?.logError({
629+
type: "sql_error",
630+
query: query,
631+
error: error instanceof Error ? error.message : String(error),
632+
timestamp: Date.now(),
633+
});
634+
635+
// You can still log to console if needed
636+
console.error(`SQL error for query: ${query}`, error);
637+
}
638+
639+
async getUserData(userId: string) {
640+
// If this query fails, onSqlError will be called automatically
641+
const users = this.sql`
642+
SELECT * FROM users WHERE id = ${userId}
643+
`;
644+
return users[0];
645+
}
646+
}
647+
```
648+
649+
</TypeScriptExample>
650+
599651
### MCP Client API
600652

601653
The Agents SDK allows your Agent to act as an MCP (Model Context Protocol) client, connecting to remote MCP servers and using their tools, resources, and prompts.

0 commit comments

Comments
 (0)