Skip to content

pipe SQL errors into the existing onError method using a new SqlError class#768

Merged
whoiskatrin merged 5 commits into
mainfrom
fix/customizable-sql-error-logger
Jan 6, 2026
Merged

pipe SQL errors into the existing onError method using a new SqlError class#768
whoiskatrin merged 5 commits into
mainfrom
fix/customizable-sql-error-logger

Conversation

@whoiskatrin

@whoiskatrin whoiskatrin commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

Instead of adding a separate onSqlError hook, I implemented a cleaner approach (suggested by @threepointone) that pipes SQL errors into the existing onError method using a new SqlError class

Fixes #767

@changeset-bot

changeset-bot Bot commented Jan 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3d7670c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@claude

claude Bot commented Jan 6, 2026

Copy link
Copy Markdown

Claude Code Review

Summary: Clean implementation that properly routes SQL errors through the existing error handling mechanism. The approach is solid and aligns well with the codebase patterns.

Issues Found: None

Analysis:

  • SqlError class properly extends Error with cause option for error chaining (packages/agents/src/index.ts:127)
  • Query is captured as readonly property for debugging
  • Error message format is clear: SQL query failed: ${message}
  • Integration with onError method maintains consistent error handling pattern
  • Changeset is appropriately marked as patch

Testing: No dedicated tests for SqlError, but the class is simple enough and implicitly tested through existing sql method tests in oauth2-mcp-client.test.ts. Consider adding explicit error case tests if SQL error handling becomes critical in production.

The PR is ready to merge.

Allow users to override onSqlError(query, error) to customize logging
behavior for SQL errors instead of using console.error directly.

Fixes #767
@whoiskatrin
whoiskatrin force-pushed the fix/customizable-sql-error-logger branch from 42ec18c to 7b72e17 Compare January 6, 2026 08:52
@pkg-pr-new

pkg-pr-new Bot commented Jan 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@768

commit: 3d7670c

agents-git-bot Bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Jan 6, 2026
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 <noreply@anthropic.com>
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Address review feedback by piping SQL errors through onError with a
SqlError class that preserves query context, rather than adding a
separate onSqlError hook.

- Add SqlError class with query and cause properties
- Update sql() catch block to throw onError(new SqlError(query, e))
- Remove onSqlError method

Users can now handle SQL errors in onError by checking instanceof SqlError.
agents-git-bot Bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Jan 6, 2026
Add documentation for the new SqlError class that is thrown when SQL
queries fail in Agents. This helps developers understand how to handle
SQL-specific errors through the onError method.

Related to cloudflare/agents#768

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@whoiskatrin whoiskatrin changed the title add onSqlError hook for customizable SQL error logging pipe SQL errors into the existing onError method using a new SqlError class Jan 6, 2026

@threepointone threepointone left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

approving pending CI

@whoiskatrin
whoiskatrin merged commit cf8a1e7 into main Jan 6, 2026
7 checks passed
@whoiskatrin
whoiskatrin deleted the fix/customizable-sql-error-logger branch January 6, 2026 17:06
@github-actions github-actions Bot mentioned this pull request Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow customizing logger for SQL errors

2 participants