@@ -456,11 +456,27 @@ public HttpClientResponse TRACE() {
456
456
return this ;
457
457
}
458
458
459
+ private RuntimeException mapException (HttpException e ) {
460
+ return errorMapper == null ? e : errorMapper .apply (e );
461
+ }
462
+
463
+ private void checkResponse (HttpResponse <?> response ) {
464
+ if (response .statusCode () >= 300 ) {
465
+ throw mapException (new HttpException (response , context ));
466
+ }
467
+ }
468
+
469
+ private void checkMaybeThrow (HttpResponse <byte []> response ) {
470
+ if (response .statusCode () >= 300 ) {
471
+ throw mapException (new HttpException (context , response ));
472
+ }
473
+ }
474
+
459
475
private void readResponseContent () {
460
476
final HttpResponse <byte []> response = sendWith (HttpResponse .BodyHandlers .ofByteArray ());
461
477
encodedResponseBody = context .readContent (response );
462
478
context .afterResponse (this );
463
- context . checkMaybeThrow (response );
479
+ checkMaybeThrow (response );
464
480
}
465
481
466
482
@ Override
@@ -542,7 +558,7 @@ public <T> Stream<T> stream(Type type) {
542
558
private <T > Stream <T > stream (BodyReader <T > bodyReader ) {
543
559
final HttpResponse <Stream <String >> res = handler (HttpResponse .BodyHandlers .ofLines ());
544
560
this .httpResponse = res ;
545
- context . checkResponse (res );
561
+ checkResponse (res );
546
562
return res .body ().map (bodyReader ::readBody );
547
563
}
548
564
@@ -626,7 +642,7 @@ protected <E> HttpResponse<Stream<E>> asyncStream(Class<E> type, HttpResponse<St
626
642
responseTimeNanos = System .nanoTime () - startAsyncNanos ;
627
643
httpResponse = response ;
628
644
context .afterResponse (this );
629
- context . checkResponse (response );
645
+ checkResponse (response );
630
646
final BodyReader <E > bodyReader = context .beanReader (type );
631
647
return new HttpWrapperResponse <>(response .body ().map (bodyReader ::readBody ), httpResponse );
632
648
}
@@ -635,7 +651,7 @@ protected <E> HttpResponse<Stream<E>> asyncStream(Type type, HttpResponse<Stream
635
651
responseTimeNanos = System .nanoTime () - startAsyncNanos ;
636
652
httpResponse = response ;
637
653
context .afterResponse (this );
638
- context . checkResponse (response );
654
+ checkResponse (response );
639
655
final BodyReader <E > bodyReader = context .beanReader (type );
640
656
return new HttpWrapperResponse <>(response .body ().map (bodyReader ::readBody ), httpResponse );
641
657
}
@@ -645,7 +661,7 @@ private void afterAsyncEncoded(HttpResponse<byte[]> response) {
645
661
httpResponse = response ;
646
662
encodedResponseBody = context .readContent (response );
647
663
context .afterResponse (this );
648
- context . checkMaybeThrow (response );
664
+ checkMaybeThrow (response );
649
665
}
650
666
651
667
protected <E > HttpResponse <E > afterAsync (HttpResponse <E > response ) {
@@ -680,7 +696,7 @@ public HttpResponse<String> asString() {
680
696
public HttpResponse <String > asPlainString () {
681
697
loggableResponseBody = true ;
682
698
final HttpResponse <String > hres = addMetrics (handler (HttpResponse .BodyHandlers .ofString ()));
683
- context . checkResponse (hres );
699
+ checkResponse (hres );
684
700
return hres ;
685
701
}
686
702
0 commit comments