Skip to content

Commit a178bbe

Browse files
committed
DefaultResponseErrorHandler shows full error details
Closes gh-27552
1 parent fcf4315 commit a178bbe

File tree

3 files changed

+16
-54
lines changed

3 files changed

+16
-54
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 & 24 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,31 +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-
if (charset == null) {
143-
charset = StandardCharsets.UTF_8;
144-
}
145-
int maxChars = 200;
140+
charset = (charset != null ? charset : StandardCharsets.UTF_8);
146141

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

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

165148
/**

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

Lines changed: 2 additions & 21 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;
@@ -74,27 +72,10 @@ public void handleError() throws Exception {
7472

7573
assertThatExceptionOfType(HttpClientErrorException.class)
7674
.isThrownBy(() -> handler.handleError(response))
77-
.withMessage("404 Not Found: [Hello World]")
75+
.withMessage("404 Not Found: \"Hello World\"")
7876
.satisfies(ex -> assertThat(ex.getResponseHeaders()).isSameAs(headers));
7977
}
8078

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-
.withMessage("404 Not Found: [" + bodyGenerator.apply(200) + "... (500 bytes)]");
96-
}
97-
9879
@Test
9980
public void handleErrorIOException() throws Exception {
10081
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)