Skip to content

Commit d39544e

Browse files
authored
fix: preserve typed error as cause in SSE errors (#315)
## 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: ```typescript 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: ```typescript const cause = error.cause as JSONRPCTransportError; const { code, message, data } = cause.errorResponse.error; ```
1 parent b8158fd commit d39544e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/client/transports/json_rpc_transport.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ export class JsonRpcTransport implements Transport {
329329
if ('error' in a2aStreamResponse) {
330330
const err = a2aStreamResponse.error;
331331
throw new Error(
332-
`SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data || {})}`
332+
`SSE event contained an error: ${err.message} (Code: ${err.code}) Data: ${JSON.stringify(err.data || {})}`,
333+
{ cause: JsonRpcTransport.mapToError(a2aStreamResponse) }
333334
);
334335
}
335336

0 commit comments

Comments
 (0)