Commit d39544e
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
1 file changed
+2
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
332 | | - | |
| 332 | + | |
| 333 | + | |
333 | 334 | | |
334 | 335 | | |
335 | 336 | | |
| |||
0 commit comments