Skip to content

Commit 21b10cc

Browse files
docs: add SqlError class documentation for SQL error handling
Document the new SqlError class that provides detailed information about failed SQL queries. This change syncs documentation for PR #768 which introduces SqlError for better error handling in the agents SDK. Changes: - Add SqlError section in API reference with properties and usage example - Add SQL Error Handling subsection under this.sql in agent-class.mdx - Update onError section to demonstrate SqlError handling - Add cross-references between related documentation sections Related: 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 21b10cc

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,60 @@ function useAgent<State = unknown>(
800800
};
801801
```
802802

803+
### SqlError
804+
805+
The `SqlError` class is thrown when a SQL query executed via `this.sql` fails. It provides detailed information about the failed query and the underlying error, allowing you to implement custom error handling logic in your Agent's `onError` method.
806+
807+
#### Properties
808+
809+
- **query** (`string`): The SQL query that failed
810+
- **cause** (`unknown`): The original error that caused the failure
811+
- **message** (`string`): A formatted error message combining both the query and cause
812+
813+
#### Example
814+
815+
<TypeScriptExample>
816+
817+
```ts
818+
import { Agent, SqlError } from "agents";
819+
820+
class MyAgent extends Agent {
821+
async performDatabaseOperation() {
822+
// SQL errors are automatically caught and passed to onError
823+
const result = this.sql`
824+
INSERT INTO users (id, email)
825+
VALUES (${userId}, ${email})
826+
`;
827+
}
828+
829+
onError(error: unknown) {
830+
if (error instanceof SqlError) {
831+
// Access the failed query
832+
console.error("Failed query:", error.query);
833+
834+
// Access the underlying error
835+
console.error("Cause:", error.cause);
836+
837+
// Implement custom logic based on the query
838+
if (error.query.includes("INSERT INTO users")) {
839+
// Handle user insertion failures
840+
// For example, check for duplicate key errors
841+
}
842+
843+
// Log to external monitoring service
844+
// this.logToMonitoring(error);
845+
}
846+
847+
// Re-throw to propagate the error
848+
throw error;
849+
}
850+
}
851+
```
852+
853+
</TypeScriptExample>
854+
855+
Learn more about SQL error handling in the [Agent class internals](/agents/concepts/agent-class/#sql-error-handling).
856+
803857
### Chat Agent
804858

805859
The Agents SDK exposes an `AIChatAgent` class that extends the `Agent` class and exposes an `onChatMessage` method that simplifies building interactive chat agents.

src/content/docs/agents/concepts/agent-class.mdx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,45 @@ class MyAgent extends Agent {
245245
}
246246
```
247247

248+
#### SQL Error Handling
249+
250+
When a SQL query fails, the `this.sql` method throws a `SqlError` instead of logging to the console. This error is automatically passed to your `onError` handler, allowing you to implement custom error handling logic.
251+
252+
The `SqlError` class includes:
253+
- `query`: The SQL query that failed
254+
- `cause`: The original error that caused the failure
255+
- `message`: A formatted error message
256+
257+
```ts
258+
import { Agent, SqlError } from "agents";
259+
260+
class MyAgent extends Agent {
261+
onStart() {
262+
try {
263+
// This query might fail
264+
this.sql`INSERT INTO users (id, name) VALUES (${userId}, ${userName})`;
265+
} catch (error) {
266+
// Errors are wrapped in SqlError and passed to onError
267+
// See the onError section below for how to handle SQL errors
268+
}
269+
}
270+
271+
onError(error: unknown) {
272+
if (error instanceof SqlError) {
273+
console.error("SQL query failed:", error.query);
274+
console.error("Original error:", error.cause);
275+
276+
// Implement custom logic based on the query or error type
277+
if (error.query.includes("INSERT")) {
278+
// Handle insert failures specifically
279+
}
280+
}
281+
282+
throw error;
283+
}
284+
}
285+
```
286+
248287
### RPC and Callable Methods
249288

250289
`agents` take Durable Objects RPC one step forward by implementing RPC through WebSockets, so clients can also call methods on the Agent directly. To make a method callable through WS, developers can use the `@callable` decorator. Methods can return a serializable value or a stream (when using `@callable({ stream: true })`).
@@ -411,15 +450,27 @@ function someUtilityFunction() {
411450

412451
`Agent` extends `Server`'s `onError` so it can be used to handle errors that are not necessarily WebSocket errors. It is called with a `Connection` or `unknown` error.
413452

453+
All SQL errors from `this.sql` are automatically wrapped in a `SqlError` and passed to `onError`, allowing you to implement centralized error handling for SQL operations.
454+
414455
```ts
456+
import { Agent, SqlError } from "agents";
457+
415458
class MyAgent extends Agent {
416459
onError(connectionOrError: Connection | unknown, error?: unknown) {
417460
if (error) {
418461
// WebSocket connection error
419462
console.error("Connection error:", error);
420463
} else {
421-
// Server error
422-
console.error("Server error:", connectionOrError);
464+
// Server error (including SQL errors)
465+
if (connectionOrError instanceof SqlError) {
466+
console.error("SQL query failed:", connectionOrError.query);
467+
console.error("Cause:", connectionOrError.cause);
468+
469+
// Implement custom SQL error handling
470+
// For example, log to external monitoring service
471+
} else {
472+
console.error("Server error:", connectionOrError);
473+
}
423474
}
424475

425476
// Optionally throw to propagate the error

0 commit comments

Comments
 (0)