Skip to content

Commit 3a16ecb

Browse files
committed
Move checkResponse() and checkMaybeThrow() to DHttpClientRequest and map error
1 parent 61067cc commit 3a16ecb

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

http-client/src/main/java/io/avaje/http/client/DHttpClientContext.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,6 @@ public long avgMicros() {
199199
}
200200
}
201201

202-
@Override
203-
public void checkResponse(HttpResponse<?> response) {
204-
if (response.statusCode() >= 300) {
205-
throw new HttpException(response, this);
206-
}
207-
}
208-
209-
void checkMaybeThrow(HttpResponse<byte[]> response) {
210-
if (response.statusCode() >= 300) {
211-
throw new HttpException(this, response);
212-
}
213-
}
214-
215202
@SuppressWarnings("unchecked")
216203
public BodyContent readErrorContent(boolean responseAsBytes, HttpResponse<?> httpResponse) {
217204
if (responseAsBytes) {

http-client/src/main/java/io/avaje/http/client/DHttpClientRequest.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,27 @@ public HttpClientResponse TRACE() {
456456
return this;
457457
}
458458

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+
459475
private void readResponseContent() {
460476
final HttpResponse<byte[]> response = sendWith(HttpResponse.BodyHandlers.ofByteArray());
461477
encodedResponseBody = context.readContent(response);
462478
context.afterResponse(this);
463-
context.checkMaybeThrow(response);
479+
checkMaybeThrow(response);
464480
}
465481

466482
@Override
@@ -542,7 +558,7 @@ public <T> Stream<T> stream(Type type) {
542558
private <T> Stream<T> stream(BodyReader<T> bodyReader) {
543559
final HttpResponse<Stream<String>> res = handler(HttpResponse.BodyHandlers.ofLines());
544560
this.httpResponse = res;
545-
context.checkResponse(res);
561+
checkResponse(res);
546562
return res.body().map(bodyReader::readBody);
547563
}
548564

@@ -626,7 +642,7 @@ protected <E> HttpResponse<Stream<E>> asyncStream(Class<E> type, HttpResponse<St
626642
responseTimeNanos = System.nanoTime() - startAsyncNanos;
627643
httpResponse = response;
628644
context.afterResponse(this);
629-
context.checkResponse(response);
645+
checkResponse(response);
630646
final BodyReader<E> bodyReader = context.beanReader(type);
631647
return new HttpWrapperResponse<>(response.body().map(bodyReader::readBody), httpResponse);
632648
}
@@ -635,7 +651,7 @@ protected <E> HttpResponse<Stream<E>> asyncStream(Type type, HttpResponse<Stream
635651
responseTimeNanos = System.nanoTime() - startAsyncNanos;
636652
httpResponse = response;
637653
context.afterResponse(this);
638-
context.checkResponse(response);
654+
checkResponse(response);
639655
final BodyReader<E> bodyReader = context.beanReader(type);
640656
return new HttpWrapperResponse<>(response.body().map(bodyReader::readBody), httpResponse);
641657
}
@@ -645,7 +661,7 @@ private void afterAsyncEncoded(HttpResponse<byte[]> response) {
645661
httpResponse = response;
646662
encodedResponseBody = context.readContent(response);
647663
context.afterResponse(this);
648-
context.checkMaybeThrow(response);
664+
checkMaybeThrow(response);
649665
}
650666

651667
protected <E> HttpResponse<E> afterAsync(HttpResponse<E> response) {
@@ -680,7 +696,7 @@ public HttpResponse<String> asString() {
680696
public HttpResponse<String> asPlainString() {
681697
loggableResponseBody = true;
682698
final HttpResponse<String> hres = addMetrics(handler(HttpResponse.BodyHandlers.ofString()));
683-
context.checkResponse(hres);
699+
checkResponse(hres);
684700
return hres;
685701
}
686702

http-client/src/main/java/io/avaje/http/client/SpiHttpClient.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,4 @@ interface SpiHttpClient {
3838
*/
3939
byte[] decodeContent(String encoding, byte[] content);
4040

41-
/**
42-
* Check the response status code and throw HttpException if the status
43-
* code is in the error range.
44-
*/
45-
void checkResponse(HttpResponse<?> response);
46-
4741
}

0 commit comments

Comments
 (0)