Skip to content

Conversation

@whoiskatrin
Copy link
Contributor

@whoiskatrin whoiskatrin commented Jan 6, 2026

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
Copy link

changeset-bot bot commented Jan 6, 2026

🦋 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
Copy link

claude bot commented Jan 6, 2026

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
Copy link

pkg-pr-new bot commented Jan 6, 2026

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
Add documentation for the new onSqlError hook that allows users to
customize SQL error logging behavior. This hook was added in
cloudflare/agents#768 to replace hardcoded console.error calls.

- Add onSqlError to the main Agent class API example
- Add dedicated SQL error handling section with API signature
- Include practical example showing integration with observability service
- Explain when the hook is called and default behavior

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

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Jan 6, 2026
Adds documentation for the new onSqlError hook that allows users to
customize SQL error logging behavior. The hook is called when SQL
queries fail, giving users full control over error handling and
logging.

Related to cloudflare/agents#768

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

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
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 <[email protected]>
// 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

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 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]>
agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Jan 6, 2026
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]>
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 <[email protected]>
@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
Copy link
Contributor

@threepointone threepointone left a comment

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