1616
1717package org .springframework .graphql .client ;
1818
19+ import java .io .IOException ;
20+ import java .nio .charset .Charset ;
1921import java .util .Collections ;
2022import java .util .Map ;
2123
24+ import org .jspecify .annotations .Nullable ;
25+
2226import org .springframework .core .ParameterizedTypeReference ;
2327import org .springframework .graphql .GraphQlRequest ;
2428import org .springframework .graphql .GraphQlResponse ;
2529import org .springframework .graphql .MediaTypes ;
2630import org .springframework .http .HttpHeaders ;
31+ import org .springframework .http .HttpInputMessage ;
32+ import org .springframework .http .HttpMessage ;
2733import org .springframework .http .HttpStatus ;
2834import org .springframework .http .MediaType ;
2935import org .springframework .http .client .ClientHttpResponse ;
3036import org .springframework .util .Assert ;
37+ import org .springframework .util .FileCopyUtils ;
3138import org .springframework .web .client .HttpClientErrorException ;
39+ import org .springframework .web .client .HttpServerErrorException ;
3240import org .springframework .web .client .RestClient ;
3341
3442
@@ -76,7 +84,16 @@ public GraphQlResponse execute(GraphQlRequest request) {
7684 else if (httpResponse .getStatusCode ().is4xxClientError () && isGraphQlResponse (httpResponse )) {
7785 return httpResponse .bodyTo (MAP_TYPE );
7886 }
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+ }
8097 });
8198 return new ResponseMapGraphQlResponse ((body != null ) ? body : Collections .emptyMap ());
8299 }
@@ -86,4 +103,19 @@ private static boolean isGraphQlResponse(ClientHttpResponse clientResponse) {
86103 .isCompatibleWith (clientResponse .getHeaders ().getContentType ());
87104 }
88105
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+
89121}
0 commit comments