16
16
17
17
package org .springframework .graphql .client ;
18
18
19
+ import java .io .IOException ;
20
+ import java .nio .charset .Charset ;
19
21
import java .util .Collections ;
20
22
import java .util .Map ;
21
23
24
+ import org .jspecify .annotations .Nullable ;
25
+
22
26
import org .springframework .core .ParameterizedTypeReference ;
23
27
import org .springframework .graphql .GraphQlRequest ;
24
28
import org .springframework .graphql .GraphQlResponse ;
25
29
import org .springframework .graphql .MediaTypes ;
26
30
import org .springframework .http .HttpHeaders ;
31
+ import org .springframework .http .HttpInputMessage ;
32
+ import org .springframework .http .HttpMessage ;
27
33
import org .springframework .http .HttpStatus ;
28
34
import org .springframework .http .MediaType ;
29
35
import org .springframework .http .client .ClientHttpResponse ;
30
36
import org .springframework .util .Assert ;
37
+ import org .springframework .util .FileCopyUtils ;
31
38
import org .springframework .web .client .HttpClientErrorException ;
39
+ import org .springframework .web .client .HttpServerErrorException ;
32
40
import org .springframework .web .client .RestClient ;
33
41
34
42
@@ -76,7 +84,16 @@ public GraphQlResponse execute(GraphQlRequest request) {
76
84
else if (httpResponse .getStatusCode ().is4xxClientError () && isGraphQlResponse (httpResponse )) {
77
85
return httpResponse .bodyTo (MAP_TYPE );
78
86
}
79
- throw new HttpClientErrorException (httpResponse .getStatusCode (), httpResponse .getStatusText ());
87
+ else if (httpResponse .getStatusCode ().is4xxClientError ()) {
88
+ throw HttpClientErrorException .create (httpResponse .getStatusText (), httpResponse .getStatusCode (),
89
+ httpResponse .getStatusText (), httpResponse .getHeaders (),
90
+ getBody (httpResponse ), getCharset (httpResponse ));
91
+ }
92
+ else {
93
+ throw HttpServerErrorException .create (httpResponse .getStatusText (), httpResponse .getStatusCode (),
94
+ httpResponse .getStatusText (), httpResponse .getHeaders (),
95
+ getBody (httpResponse ), getCharset (httpResponse ));
96
+ }
80
97
});
81
98
return new ResponseMapGraphQlResponse ((body != null ) ? body : Collections .emptyMap ());
82
99
}
@@ -86,4 +103,19 @@ private static boolean isGraphQlResponse(ClientHttpResponse clientResponse) {
86
103
.isCompatibleWith (clientResponse .getHeaders ().getContentType ());
87
104
}
88
105
106
+ private static byte [] getBody (HttpInputMessage message ) {
107
+ try {
108
+ return FileCopyUtils .copyToByteArray (message .getBody ());
109
+ }
110
+ catch (IOException ignore ) {
111
+ }
112
+ return new byte [0 ];
113
+ }
114
+
115
+ private static @ Nullable Charset getCharset (HttpMessage response ) {
116
+ HttpHeaders headers = response .getHeaders ();
117
+ MediaType contentType = headers .getContentType ();
118
+ return (contentType != null ) ? contentType .getCharset () : null ;
119
+ }
120
+
89
121
}
0 commit comments