@@ -224,7 +224,7 @@ private static HttpClientHandler CreateHandler(List<X509Certificate2> certificat
224224 handler . ClientCertificateOptions = ClientCertificateOption . Manual ;
225225 certificates . ForEach ( c => handler . ClientCertificates . Add ( c ) ) ;
226226 }
227-
227+
228228 return handler ;
229229 }
230230
@@ -279,6 +279,7 @@ private async Task<HttpResponseMessage> ExecuteOnOneInstanceAsync(Func<Task<Http
279279 // concurrent access).
280280
281281 string aggregatedErrorMessage = null ;
282+ List < Exception > aggregatedExceptions = new List < Exception > ( ) ;
282283 HttpResponseMessage response = null ;
283284 bool firstError = true ;
284285
@@ -314,26 +315,39 @@ private async Task<HttpResponseMessage> ExecuteOnOneInstanceAsync(Func<Task<Http
314315
315316 if ( ! IsRetriable ( ( int ) response . StatusCode ) )
316317 {
317- try
318- {
319- JObject errorObject = null ;
320- errorObject = JObject . Parse (
321- await response . Content . ReadAsStringAsync ( )
322- . ConfigureAwait ( continueOnCapturedContext : false ) ) ;
323- message = errorObject . Value < string > ( "message" ) ;
324- errorCode = errorObject . Value < int > ( "error_code" ) ;
325- }
326- catch ( Exception )
318+ string content = await response . Content . ReadAsStringAsync ( )
319+ . ConfigureAwait ( continueOnCapturedContext : false ) ;
320+
321+ if ( ! string . IsNullOrWhiteSpace ( content ) )
327322 {
328- // consider an unauthorized response from any server to be conclusive.
329- if ( response . StatusCode == HttpStatusCode . Unauthorized )
323+ try
330324 {
331- finished = true ;
332- throw new HttpRequestException ( $ "Unauthorized") ;
325+ var errorObject = JObject . Parse ( content ) ;
326+ message = errorObject . Value < string > ( "message" ) ;
327+ errorCode = errorObject . Value < int > ( "error_code" ) ;
328+
329+ throw new SchemaRegistryException ( message , response . StatusCode , errorCode ) ;
330+ }
331+ catch ( JsonReaderException )
332+ {
333+ // content isn’t json
334+ throw new HttpRequestException (
335+ $ "Unexpected non-JSON response from server ({ ( int ) response . StatusCode } { response . StatusCode } ): { content } "
336+ ) ;
337+ }
338+ catch
339+ {
340+ if ( response . StatusCode == HttpStatusCode . Unauthorized )
341+ {
342+ finished = true ;
343+ throw new HttpRequestException ( "Unauthorized" ) ;
344+ }
333345 }
334346 }
335347
336- throw new SchemaRegistryException ( message , response . StatusCode , errorCode ) ;
348+ // no content
349+ throw new HttpRequestException (
350+ $ "Server returned { ( int ) response . StatusCode } ({ response . StatusCode } ) with no content.") ;
337351 }
338352
339353 if ( ! firstError )
@@ -351,9 +365,10 @@ await response.Content.ReadAsStringAsync()
351365 message = errorObject . Value < string > ( "message" ) ;
352366 errorCode = errorObject . Value < int > ( "error_code" ) ;
353367 }
354- catch
368+ catch ( Exception e )
355369 {
356370 aggregatedErrorMessage += $ "[{ clients [ clientIndex ] . BaseAddress } ] { response . StatusCode } ";
371+ aggregatedExceptions . Add ( e ) ;
357372 }
358373
359374 aggregatedErrorMessage +=
@@ -375,10 +390,16 @@ await response.Content.ReadAsStringAsync()
375390 firstError = false ;
376391
377392 aggregatedErrorMessage += $ "[{ clients [ clientIndex ] . BaseAddress } ] HttpRequestException: { e . Message } ";
393+ aggregatedExceptions . Add ( e ) ;
378394 }
379395 }
380396
381- throw new HttpRequestException ( aggregatedErrorMessage ) ;
397+ AggregateException aggregatedException = null ;
398+ if ( aggregatedExceptions . Count > 0 )
399+ {
400+ aggregatedException = new AggregateException ( aggregatedErrorMessage , aggregatedExceptions ) ;
401+ }
402+ throw new HttpRequestException ( aggregatedErrorMessage , aggregatedException ) ;
382403 }
383404
384405 private async Task < HttpResponseMessage > SendRequest (
0 commit comments