@@ -158,23 +158,20 @@ export class A2AClient {
158158
159159 if ( ! httpResponse . ok ) {
160160 let errorBodyText = '(empty or non-JSON response)' ;
161+ let errorJson : any = { } ;
161162 try {
162163 errorBodyText = await httpResponse . text ( ) ;
163- const errorJson = JSON . parse ( errorBodyText ) ;
164- // If the body is a valid JSON-RPC error response, return it as a proper JSON-RPC error response.
165- if ( errorJson . jsonrpc && errorJson . error ) {
166- return errorJson as TResponse ;
167- } else if ( ! errorJson . jsonrpc && errorJson . error ) { // Check if it's a JSON-RPC error structure
168- throw new Error ( `RPC error for ${ method } : ${ errorJson . error . message } (Code: ${ errorJson . error . code } , HTTP Status: ${ httpResponse . status } ) Data: ${ JSON . stringify ( errorJson . error . data || { } ) } ` ) ;
169- } else if ( ! errorJson . jsonrpc ) {
170- throw new Error ( `HTTP error for ${ method } ! Status: ${ httpResponse . status } ${ httpResponse . statusText } . Response: ${ errorBodyText } ` ) ;
171- }
164+ errorJson = JSON . parse ( errorBodyText ) ;
172165 } catch ( e : any ) {
173- // If parsing the error body fails or it's not a JSON-RPC error, throw a generic HTTP error.
174- // If it was already an error thrown from within the try block, rethrow it.
175- if ( e . message . startsWith ( 'RPC error for' ) || e . message . startsWith ( 'HTTP error for' ) ) throw e ;
176- throw new Error ( `HTTP error for ${ method } ! Status: ${ httpResponse . status } ${ httpResponse . statusText } . Response: ${ errorBodyText } ` ) ;
166+ throw new Error ( `HTTP error for ${ method } ! Status: ${ httpResponse . status } ${ httpResponse . statusText } . Response: ${ errorBodyText } ` , { cause : e } ) ;
167+ }
168+ // If the body is a valid JSON-RPC error response, return it as a proper JSON-RPC error response.
169+ if ( errorJson . jsonrpc && errorJson . error ) {
170+ return errorJson as TResponse ;
171+ } else if ( ! errorJson . jsonrpc && errorJson . error ) { // Check if it's a JSON-RPC error structure
172+ throw new Error ( `RPC error for ${ method } : ${ errorJson . error . message } (Code: ${ errorJson . error . code } , HTTP Status: ${ httpResponse . status } ) Data: ${ JSON . stringify ( errorJson . error . data || { } ) } ` ) ;
177173 }
174+ throw new Error ( `HTTP error for ${ method } ! Status: ${ httpResponse . status } ${ httpResponse . statusText } . Response: ${ errorBodyText } ` ) ;
178175 }
179176
180177 const rpcResponse = await httpResponse . json ( ) ;
@@ -250,16 +247,15 @@ export class A2AClient {
250247 if ( ! response . ok ) {
251248 // Attempt to read error body for more details
252249 let errorBody = "" ;
250+ let errorJson : any = { } ;
253251 try {
254252 errorBody = await response . text ( ) ;
255- const errorJson = JSON . parse ( errorBody ) ;
256- if ( errorJson . error ) {
257- throw new Error ( `HTTP error establishing stream for message/stream: ${ response . status } ${ response . statusText } . RPC Error: ${ errorJson . error . message } (Code: ${ errorJson . error . code } )` ) ;
258- }
253+ errorJson = JSON . parse ( errorBody ) ;
259254 } catch ( e : any ) {
260- if ( e . message . startsWith ( 'HTTP error establishing stream' ) ) throw e ;
261- // Fallback if body is not JSON or parsing fails
262- throw new Error ( `HTTP error establishing stream for message/stream: ${ response . status } ${ response . statusText } . Response: ${ errorBody || '(empty)' } ` ) ;
255+ throw new Error ( `HTTP error establishing stream for message/stream: ${ response . status } ${ response . statusText } . Response: ${ errorBody || '(empty)' } ` , { cause : e } ) ;
256+ }
257+ if ( errorJson . error ) {
258+ throw new Error ( `HTTP error establishing stream for message/stream: ${ response . status } ${ response . statusText } . RPC Error: ${ errorJson . error . message } (Code: ${ errorJson . error . code } )` ) ;
263259 }
264260 throw new Error ( `HTTP error establishing stream for message/stream: ${ response . status } ${ response . statusText } ` ) ;
265261 }
@@ -393,15 +389,16 @@ export class A2AClient {
393389
394390 if ( ! response . ok ) {
395391 let errorBody = "" ;
392+ let errorJson : any = { } ;
396393 try {
397394 errorBody = await response . text ( ) ;
398- const errorJson = JSON . parse ( errorBody ) ;
399- if ( errorJson . error ) {
400- throw new Error ( `HTTP error establishing stream for tasks/resubscribe: ${ response . status } ${ response . statusText } . RPC Error: ${ errorJson . error . message } (Code: ${ errorJson . error . code } )` ) ;
401- }
395+ errorJson = JSON . parse ( errorBody ) ;
402396 } catch ( e : any ) {
403397 if ( e . message . startsWith ( 'HTTP error establishing stream' ) ) throw e ;
404- throw new Error ( `HTTP error establishing stream for tasks/resubscribe: ${ response . status } ${ response . statusText } . Response: ${ errorBody || '(empty)' } ` ) ;
398+ throw new Error ( `HTTP error establishing stream for tasks/resubscribe: ${ response . status } ${ response . statusText } . Response: ${ errorBody || '(empty)' } ` , { cause : e } ) ;
399+ }
400+ if ( errorJson . error ) {
401+ throw new Error ( `HTTP error establishing stream for tasks/resubscribe: ${ response . status } ${ response . statusText } . RPC Error: ${ errorJson . error . message } (Code: ${ errorJson . error . code } )` ) ;
405402 }
406403 throw new Error ( `HTTP error establishing stream for tasks/resubscribe: ${ response . status } ${ response . statusText } ` ) ;
407404 }
0 commit comments