|
17 | 17 | package org.springframework.ai.mistralai.api; |
18 | 18 |
|
19 | 19 | import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.Map; |
22 | 23 | import java.util.function.Consumer; |
|
38 | 39 | import org.springframework.http.client.ClientHttpResponse; |
39 | 40 | import org.springframework.util.Assert; |
40 | 41 | import org.springframework.util.CollectionUtils; |
| 42 | +import org.springframework.util.StreamUtils; |
41 | 43 | import org.springframework.web.client.ResponseErrorHandler; |
42 | 44 | import org.springframework.web.client.RestClient; |
43 | 45 | import org.springframework.web.reactive.function.client.WebClient; |
@@ -106,13 +108,12 @@ public boolean hasError(ClientHttpResponse response) throws IOException { |
106 | 108 | @Override |
107 | 109 | public void handleError(ClientHttpResponse response) throws IOException { |
108 | 110 | if (response.getStatusCode().isError()) { |
| 111 | + String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); |
| 112 | + String message = String.format("%s - %s", response.getStatusCode().value(), error); |
109 | 113 | if (response.getStatusCode().is4xxClientError()) { |
110 | | - throw new MistralAiApiClientErrorException(String.format("%s - %s", |
111 | | - response.getStatusCode().value(), |
112 | | - MistralAiApi.this.objectMapper.readValue(response.getBody(), ResponseError.class))); |
| 114 | + throw new MistralAiApiClientErrorException(message); |
113 | 115 | } |
114 | | - throw new MistralAiApiException(String.format("%s - %s", response.getStatusCode().value(), |
115 | | - MistralAiApi.this.objectMapper.readValue(response.getBody(), ResponseError.class))); |
| 116 | + throw new MistralAiApiException(message); |
116 | 117 | } |
117 | 118 | } |
118 | 119 | }; |
@@ -150,33 +151,6 @@ public MistralAiApiClientErrorException(String message) { |
150 | 151 |
|
151 | 152 | } |
152 | 153 |
|
153 | | - /** |
154 | | - * API error response. |
155 | | - * |
156 | | - * @param error Error details. |
157 | | - */ |
158 | | - @JsonInclude(Include.NON_NULL) |
159 | | - public record ResponseError(@JsonProperty("error") Error error) { |
160 | | - |
161 | | - /** |
162 | | - * Error details. |
163 | | - * |
164 | | - * @param message Error message. |
165 | | - * @param type Error type. |
166 | | - * @param param Error parameter. |
167 | | - * @param code Error code. |
168 | | - */ |
169 | | - @JsonInclude(Include.NON_NULL) |
170 | | - public record Error( |
171 | | - // @formatter:off |
172 | | - @JsonProperty("message") String message, |
173 | | - @JsonProperty("type") String type, |
174 | | - @JsonProperty("param") String param, |
175 | | - @JsonProperty("code") String code) { |
176 | | - // @formatter:on |
177 | | - } |
178 | | - } |
179 | | - |
180 | 154 | /** |
181 | 155 | * Usage statistics. |
182 | 156 | * |
|
0 commit comments