Skip to content

Commit dfa486b

Browse files
committed
Fix missing response body in client exceptions
As of gh-1117, the `HttpSyncGraphQlClient` supports the "GraphQL over HTTP" specification. More specifically, it now handles HTTP 4xx responses sent by GraphQL servers when the response content type is "applcation/graphql-response+json". This change introduced a regression where 4xx and 5xx HTTP responses now always throw `HttpClientErrorException` (instead of throwing `HttpClientErrorException` or `HttpServerErrorException` depending on the case), and this exception is missing the response body and other information from the response. This commit ensures that exceptions are thrown in a similar fashion to the default `StatusHandler` from Framwork. Fixes gh-1259
1 parent d01b55d commit dfa486b

File tree

2 files changed

+211
-136
lines changed

2 files changed

+211
-136
lines changed

spring-graphql/src/main/java/org/springframework/graphql/client/HttpSyncGraphQlTransport.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.graphql.client;
1818

19+
import java.io.IOException;
20+
import java.nio.charset.Charset;
1921
import java.util.Collections;
2022
import java.util.Map;
2123

@@ -24,11 +26,16 @@
2426
import org.springframework.graphql.GraphQlResponse;
2527
import org.springframework.graphql.MediaTypes;
2628
import org.springframework.http.HttpHeaders;
29+
import org.springframework.http.HttpInputMessage;
30+
import org.springframework.http.HttpMessage;
2731
import org.springframework.http.HttpStatus;
2832
import org.springframework.http.MediaType;
2933
import org.springframework.http.client.ClientHttpResponse;
34+
import org.springframework.lang.Nullable;
3035
import org.springframework.util.Assert;
36+
import org.springframework.util.FileCopyUtils;
3137
import org.springframework.web.client.HttpClientErrorException;
38+
import org.springframework.web.client.HttpServerErrorException;
3239
import org.springframework.web.client.RestClient;
3340

3441

@@ -75,7 +82,16 @@ public GraphQlResponse execute(GraphQlRequest request) {
7582
else if (httpResponse.getStatusCode().is4xxClientError() && isGraphQlResponse(httpResponse)) {
7683
return httpResponse.bodyTo(MAP_TYPE);
7784
}
78-
throw new HttpClientErrorException(httpResponse.getStatusCode(), httpResponse.getStatusText());
85+
else if (httpResponse.getStatusCode().is4xxClientError()) {
86+
throw HttpClientErrorException.create(httpResponse.getStatusText(), httpResponse.getStatusCode(),
87+
httpResponse.getStatusText(), httpResponse.getHeaders(),
88+
getBody(httpResponse), getCharset(httpResponse));
89+
}
90+
else {
91+
throw HttpServerErrorException.create(httpResponse.getStatusText(), httpResponse.getStatusCode(),
92+
httpResponse.getStatusText(), httpResponse.getHeaders(),
93+
getBody(httpResponse), getCharset(httpResponse));
94+
}
7995
});
8096
return new ResponseMapGraphQlResponse((body != null) ? body : Collections.emptyMap());
8197
}
@@ -85,4 +101,20 @@ private static boolean isGraphQlResponse(ClientHttpResponse clientResponse) {
85101
.isCompatibleWith(clientResponse.getHeaders().getContentType());
86102
}
87103

104+
private static byte[] getBody(HttpInputMessage message) {
105+
try {
106+
return FileCopyUtils.copyToByteArray(message.getBody());
107+
}
108+
catch (IOException ignore) {
109+
}
110+
return new byte[0];
111+
}
112+
113+
@Nullable
114+
private static Charset getCharset(HttpMessage response) {
115+
HttpHeaders headers = response.getHeaders();
116+
MediaType contentType = headers.getContentType();
117+
return (contentType != null) ? contentType.getCharset() : null;
118+
}
119+
88120
}

0 commit comments

Comments
 (0)