Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-donkeys-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

pipe SQL errors into the existing onError method using a new SqlError class
18 changes: 16 additions & 2 deletions packages/agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ export type CallableMetadata = {

const callableMetadata = new Map<Function, CallableMetadata>();

/**
* Error class for SQL execution failures, containing the query that failed
*/
export class SqlError extends Error {
/** The SQL query that failed */
readonly query: string;

constructor(query: string, cause: unknown) {
const message = cause instanceof Error ? cause.message : String(cause);
super(`SQL query failed: ${message}`, { cause });
this.name = "SqlError";
this.query = query;
}
}

/**
* Decorator that marks a method as callable by clients
* @param metadata Optional metadata about the callable method
Expand Down Expand Up @@ -391,8 +406,7 @@ export class Agent<
// Execute the SQL query with the provided values
return [...this.ctx.storage.sql.exec(query, ...values)] as T[];
} catch (e) {
console.error(`failed to execute sql query: ${query}`, e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead why don't we use our logger directly here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or pipe into onError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with piping, done that

throw this.onError(e);
throw this.onError(new SqlError(query, e));
}
}
constructor(ctx: AgentContext, env: Env) {
Expand Down
Loading