Skip to content

fix: Catch handler errors instead of killing the connection (#131)#114

Merged
benbrandt merged 2 commits intoagentclientprotocol:mainfrom
nikomatsakis:catch-handler-errors
Apr 16, 2026
Merged

fix: Catch handler errors instead of killing the connection (#131)#114
benbrandt merged 2 commits intoagentclientprotocol:mainfrom
nikomatsakis:catch-handler-errors

Conversation

@nikomatsakis
Copy link
Copy Markdown
Contributor

Instead of propagating handler errors via ? (which tears down the connection), catch them in dispatch_dispatch and report them back to the remote side:

  • Requests: send a JSON-RPC error response with the original request id
  • Notifications: send an out-of-band error notification
  • The connection stays alive for subsequent messages

This is an alternative to the match_request/match_notification API approach. Rather than changing the handler API, errors are caught at the dispatch level — like a web server catching unhandled exceptions.

Also migrates parse_message impls from json_cast to json_cast_params so deserialization failures produce -32602 (Invalid params) instead of -32700 (Parse error), which is the correct JSON-RPC error code.

Instead of propagating handler errors via ? (which tears down the
connection), catch them in dispatch_dispatch and report them back to
the remote side:

- Requests: send a JSON-RPC error response with the original request id
- Notifications: send an out-of-band error notification
- The connection stays alive for subsequent messages

This is an alternative to the match_request/match_notification API
approach. Rather than changing the handler API, errors are caught at
the dispatch level — like a web server catching unhandled exceptions.

Also migrates parse_message impls from json_cast to json_cast_params
so deserialization failures produce -32602 (Invalid params) instead of
-32700 (Parse error), which is the correct JSON-RPC error code.
@nikomatsakis nikomatsakis requested a review from benbrandt April 16, 2026 15:59
assert_eq!(response["id"], 1);
assert!(response["error"].is_object(), "Expected error object");
assert_eq!(
response["error"]["code"], -32602,
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.

compare against the constant defined in the error type, I think

Copy link
Copy Markdown
Member

@benbrandt benbrandt left a comment

Choose a reason for hiding this comment

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

This is great and much simpler ❤️ thank you!

@benbrandt benbrandt merged commit 4444454 into agentclientprotocol:main Apr 16, 2026
1 check passed
@acp-release-bot acp-release-bot bot mentioned this pull request Apr 13, 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.

2 participants