Skip to content

fix: preserve typed error as cause in SSE errors#315

Merged
ishymko merged 1 commit intoa2aproject:mainfrom
birdayz:fix/sse-error-cause
Jan 27, 2026
Merged

fix: preserve typed error as cause in SSE errors#315
ishymko merged 1 commit intoa2aproject:mainfrom
birdayz:fix/sse-error-cause

Conversation

@birdayz
Copy link
Contributor

@birdayz birdayz commented Jan 27, 2026

What

Wrap the typed error from mapToError() as the cause when throwing SSE stream errors.

Why

When an SSE stream contains a JSON-RPC error, we throw an Error with a formatted message string. The structured error data (code, message, data) is serialized into that string and lost. Consumers who need the structured data have to parse it back out with regex.

Implementation details

Use the standard JS error cause option to preserve the typed error:

throw new Error(
  `SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data || {})}`,
  { cause: JsonRpcTransport.mapToError(a2aStreamResponse as JSONRPCErrorResponse) }
);

Backward compatible - the message string is unchanged. Consumers can access structured data via:

const cause = error.cause as JSONRPCTransportError;
const { code, message, data } = cause.errorResponse.error;

@birdayz birdayz requested a review from a team as a code owner January 27, 2026 17:40
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @birdayz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request improves error handling within the JsonRpcTransport by ensuring that when an SSE stream encounters a JSON-RPC error, the original structured error object is not merely converted into a string but is instead preserved as the cause property of the thrown Error. This allows for programmatic access to the detailed error information, making error handling more robust and developer-friendly without breaking existing integrations.

Highlights

  • Preservation of Typed Errors: The pull request modifies the JsonRpcTransport to preserve the original typed error from mapToError() as the cause when throwing SSE stream errors. This prevents the loss of structured error data (code, message, data) that previously occurred when it was only serialized into a message string.
  • Enhanced Error Accessibility: Consumers of the API can now access the structured error data directly via the error.cause property, which will be an instance of JSONRPCTransportError, eliminating the need for regex parsing of the error message string.
  • Backward Compatibility: The change is backward compatible as the original error message string remains unchanged, ensuring existing error handling logic that relies on the message string will continue to function as before.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link

github-actions bot commented Jan 27, 2026

🧪 Code Coverage

⬇️ Download Full Report

Main PR Delta
src/client/transports/json_rpc_transport.ts 60.79% 60.62% 🔴 -0.17%
Total 80.06% 80.04% 🔴 -0.02%

Generated by coverage-comment.yml

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request modifies src/client/transports/json_rpc_transport.ts to enhance error handling. Specifically, when an SSE event contains an error, the thrown Error object now includes a cause property. This cause is populated by mapping the JSONRPCErrorResponse using JsonRpcTransport.mapToError, providing more detailed context for the error.

@birdayz birdayz changed the title json_rpc_transport: preserve typed error as cause in SSE errors fix: preserve typed error as cause in SSE errors Jan 27, 2026
Copy link
Member

@ishymko ishymko left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

Error handling definitely requires improvements here (i.e. using predefined transport-agnostic errors across all transports in a consistent way and documenting them at the client level). It'll hopefully improve for 1.0 release as we'll be able to drop some backward compatible leftovers from #169 required for #198.

@birdayz birdayz force-pushed the fix/sse-error-cause branch from 2ce0057 to 90b3a60 Compare January 27, 2026 18:38
When an SSE stream contains a JSON-RPC error, we throw an Error with
a formatted message string. The structured error data (code, message,
data) is lost, forcing consumers to parse it back out of the string.

Wrap the typed error from mapToError() as the cause. This preserves
backward compatibility (same message string) while exposing the
structured error via error.cause.errorResponse.error.

Consumers can now do:
  const cause = error.cause as JSONRPCTransportError;
  const { code, message, data } = cause.errorResponse.error;
@birdayz birdayz force-pushed the fix/sse-error-cause branch from 90b3a60 to 4d02052 Compare January 27, 2026 18:40
@ishymko ishymko merged commit d39544e into a2aproject:main Jan 27, 2026
15 checks passed
ishymko pushed a commit that referenced this pull request Feb 3, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.3.10](v0.3.9...v0.3.10)
(2026-01-27)


### Bug Fixes

* do not use ReadableStream async iterator in SSE stream parsing
([#311](#311))
([5359fa8](5359fa8))
* preserve typed error as cause in SSE errors
([#315](#315))
([d39544e](d39544e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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