Skip to content

Commit 26af506

Browse files
committed
DefaultResponseErrorHandler shows full error details
Closes gh-27552
1 parent b14a940 commit 26af506

File tree

3 files changed

+16
-53
lines changed

3 files changed

+16
-53
lines changed

spring-core/src/main/java/org/springframework/core/log/LogFormatUtils.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,21 @@ public static String formatValue(@Nullable Object value, int maxLength, boolean
6060
return "";
6161
}
6262
String result;
63-
if (value instanceof CharSequence) {
64-
result = "\"" + value + "\"";
63+
try {
64+
result = value.toString();
6565
}
66-
else {
67-
try {
68-
result = value.toString();
69-
}
70-
catch (Throwable ex) {
71-
result = ex.toString();
72-
}
66+
catch (Throwable ex) {
67+
result = ex.toString();
7368
}
7469
if (maxLength != -1) {
7570
result = (result.length() > maxLength ? result.substring(0, maxLength) + " (truncated)..." : result);
7671
}
7772
if (replaceNewlines) {
7873
result = result.replace("\n", "<LF>").replace("\r", "<CR>");
7974
}
75+
if (value instanceof CharSequence) {
76+
result = "\"" + result + "\"";
77+
}
8078
return result;
8179
}
8280

spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,11 @@
1616

1717
package org.springframework.web.client;
1818

19-
import java.io.ByteArrayInputStream;
2019
import java.io.IOException;
21-
import java.io.InputStreamReader;
22-
import java.io.Reader;
23-
import java.nio.CharBuffer;
2420
import java.nio.charset.Charset;
2521
import java.nio.charset.StandardCharsets;
2622

23+
import org.springframework.core.log.LogFormatUtils;
2724
import org.springframework.http.HttpHeaders;
2825
import org.springframework.http.HttpStatus;
2926
import org.springframework.http.MediaType;
@@ -135,29 +132,17 @@ private String getErrorMessage(
135132
int rawStatusCode, String statusText, @Nullable byte[] responseBody, @Nullable Charset charset) {
136133

137134
String preface = rawStatusCode + " " + statusText + ": ";
135+
138136
if (ObjectUtils.isEmpty(responseBody)) {
139137
return preface + "[no body]";
140138
}
141139

142-
charset = charset == null ? StandardCharsets.UTF_8 : charset;
143-
int maxChars = 200;
140+
charset = (charset != null ? charset : StandardCharsets.UTF_8);
144141

145-
if (responseBody.length < maxChars * 2) {
146-
return preface + "[" + new String(responseBody, charset) + "]";
147-
}
142+
String bodyText = new String(responseBody, charset);
143+
bodyText = LogFormatUtils.formatValue(bodyText, -1, true);
148144

149-
try {
150-
Reader reader = new InputStreamReader(new ByteArrayInputStream(responseBody), charset);
151-
CharBuffer buffer = CharBuffer.allocate(maxChars);
152-
reader.read(buffer);
153-
reader.close();
154-
buffer.flip();
155-
return preface + "[" + buffer.toString() + "... (" + responseBody.length + " bytes)]";
156-
}
157-
catch (IOException ex) {
158-
// should never happen
159-
throw new IllegalStateException(ex);
160-
}
145+
return preface + bodyText;
161146
}
162147

163148
/**

spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,10 +19,8 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
2121
import java.nio.charset.StandardCharsets;
22-
import java.util.function.Function;
2322

2423
import org.junit.jupiter.api.Test;
25-
import reactor.core.publisher.Flux;
2624

2725
import org.springframework.http.HttpHeaders;
2826
import org.springframework.http.HttpStatus;
@@ -75,25 +73,7 @@ public void handleError() throws Exception {
7573
assertThatExceptionOfType(HttpClientErrorException.class)
7674
.isThrownBy(() -> handler.handleError(response))
7775
.satisfies(ex -> assertThat(ex.getResponseHeaders()).isSameAs(headers))
78-
.satisfies(ex -> assertThat(ex.getMessage()).isEqualTo("404 Not Found: [Hello World]"));
79-
}
80-
81-
@Test
82-
public void handleErrorWithLongBody() throws Exception {
83-
84-
Function<Integer, String> bodyGenerator =
85-
size -> Flux.just("a").repeat(size-1).reduce((s, s2) -> s + s2).block();
86-
87-
given(response.getRawStatusCode()).willReturn(HttpStatus.NOT_FOUND.value());
88-
given(response.getStatusText()).willReturn("Not Found");
89-
given(response.getHeaders()).willReturn(new HttpHeaders());
90-
given(response.getBody()).willReturn(
91-
new ByteArrayInputStream(bodyGenerator.apply(500).getBytes(StandardCharsets.UTF_8)));
92-
93-
assertThatExceptionOfType(HttpClientErrorException.class)
94-
.isThrownBy(() -> handler.handleError(response))
95-
.satisfies(ex -> assertThat(ex.getMessage()).isEqualTo(
96-
"404 Not Found: [" + bodyGenerator.apply(200) + "... (500 bytes)]"));
76+
.withMessage("404 Not Found: \"Hello World\"");
9777
}
9878

9979
@Test

0 commit comments

Comments
 (0)