@@ -54,6 +54,7 @@ import java.util.List;
5454import java.util.Arrays;
5555import java.util.ArrayList;
5656import java.util.Date;
57+ import java.util.Locale;
5758import java.util.stream.Collectors;
5859import java.util.stream.Stream;
5960{ {#jsr310} }
@@ -1196,6 +1197,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
11961197 * @param authNames The authentications to apply
11971198 * @param returnType The return type into which to deserialize the response
11981199 * @param isBodyNullable True if the body is nullable
1200+ * @param errorTypes Mapping of error codes to types into which to deserialize the response
11991201 * @return The response body in type of string
12001202 * @throws ApiException API exception
12011203 */
@@ -1212,7 +1214,9 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
12121214 String contentType,
12131215 String[] authNames,
12141216 GenericType<T > returnType,
1215- boolean isBodyNullable)
1217+ boolean isBodyNullable,
1218+ Map<String , GenericType > errorTypes
1219+ )
12161220 throws ApiException {
12171221
12181222 String targetURL;
@@ -1223,7 +1227,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
12231227 if (index < 0 || index >= serverConfigurations.size()) {
12241228 throw new ArrayIndexOutOfBoundsException(
12251229 String.format(
1226- java.util. Locale.ROOT,
1230+ Locale.ROOT,
12271231 " Invalid index %d when selecting the host settings. Must be less than %d" ,
12281232 index, serverConfigurations.size()));
12291233 }
@@ -1333,14 +1337,16 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
13331337 String respBody = null;
13341338 if (response.hasEntity()) {
13351339 try {
1340+ // call bufferEntity, so that a subsequent call to `readEntity` in `deserialize` doesn' t fail
1341+ response.bufferEntity();
13361342 respBody = String.valueOf(response.readEntity(String.class));
13371343 message = respBody;
13381344 } catch (RuntimeException e) {
13391345 // e.printStackTrace();
13401346 }
13411347 }
13421348 throw new ApiException(
1343- response.getStatus(), message, buildResponseHeaders(response), respBody);
1349+ response.getStatus(), message, buildResponseHeaders(response), respBody, deserializeErrorEntity(errorTypes, response) );
13441350 }
13451351 } finally {
13461352 try {
@@ -1351,6 +1357,21 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
13511357 }
13521358 }
13531359 }
1360+
1361+ private Object deserializeErrorEntity(Map<String, GenericType> errorTypes, Response response) {
1362+ if (errorTypes == null) {
1363+ return null;
1364+ }
1365+ GenericType errorType = errorTypes.get(String.valueOf(response.getStatus()));
1366+ if (errorType == null) {
1367+ errorType = errorTypes.get("0"); // "0" is the "default" response
1368+ }
1369+ try {
1370+ return deserialize(response, errorType);
1371+ } catch (Exception e) {
1372+ return String.format("Failed deserializing error entity: %s", e.toString());
1373+ }
1374+ }
13541375
13551376 protected Response sendRequest(String method, Invocation.Builder invocationBuilder, Entity<?> entity) {
13561377 Response response;
@@ -1377,7 +1398,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
13771398 */
13781399 @Deprecated
13791400 public <T> ApiResponse<T> invokeAPI(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, String> cookieParams, Map<String, Object> formParams, String accept, String contentType, String[] authNames, GenericType<T> returnType, boolean isBodyNullable) throws ApiException {
1380- return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable);
1401+ return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable, null/*TODO SME manage*/ );
13811402 }
13821403
13831404 /**
0 commit comments