6363import java .util .Arrays ;
6464import java .util .ArrayList ;
6565import java .util .Date ;
66+ import java .util .Locale ;
6667import java .util .stream .Collectors ;
6768import java .util .stream .Stream ;
6869import java .time .OffsetDateTime ;
@@ -1197,6 +1198,7 @@ public File prepareDownloadFile(Response response) throws IOException {
11971198 * @param authNames The authentications to apply
11981199 * @param returnType The return type into which to deserialize the response
11991200 * @param isBodyNullable True if the body is nullable
1201+ * @param errorTypes Mapping of error codes to types into which to deserialize the response
12001202 * @return The response body in type of string
12011203 * @throws ApiException API exception
12021204 */
@@ -1213,7 +1215,9 @@ public <T> ApiResponse<T> invokeAPI(
12131215 String contentType ,
12141216 String [] authNames ,
12151217 GenericType <T > returnType ,
1216- boolean isBodyNullable )
1218+ boolean isBodyNullable ,
1219+ Map <String , GenericType > errorTypes
1220+ )
12171221 throws ApiException {
12181222
12191223 String targetURL ;
@@ -1224,7 +1228,7 @@ public <T> ApiResponse<T> invokeAPI(
12241228 if (index < 0 || index >= serverConfigurations .size ()) {
12251229 throw new ArrayIndexOutOfBoundsException (
12261230 String .format (
1227- java . util . Locale .ROOT ,
1231+ Locale .ROOT ,
12281232 "Invalid index %d when selecting the host settings. Must be less than %d" ,
12291233 index , serverConfigurations .size ()));
12301234 }
@@ -1327,14 +1331,16 @@ public <T> ApiResponse<T> invokeAPI(
13271331 String respBody = null ;
13281332 if (response .hasEntity ()) {
13291333 try {
1334+ // call bufferEntity, so that a subsequent call to `readEntity` in `deserialize` doesn't fail
1335+ response .bufferEntity ();
13301336 respBody = String .valueOf (response .readEntity (String .class ));
13311337 message = respBody ;
13321338 } catch (RuntimeException e ) {
13331339 // e.printStackTrace();
13341340 }
13351341 }
13361342 throw new ApiException (
1337- response .getStatus (), message , buildResponseHeaders (response ), respBody );
1343+ response .getStatus (), message , buildResponseHeaders (response ), respBody , deserializeErrorEntity ( errorTypes , response ) );
13381344 }
13391345 } finally {
13401346 try {
@@ -1345,6 +1351,21 @@ public <T> ApiResponse<T> invokeAPI(
13451351 }
13461352 }
13471353 }
1354+
1355+ private Object deserializeErrorEntity (Map <String , GenericType > errorTypes , Response response ) {
1356+ if (errorTypes == null ) {
1357+ return null ;
1358+ }
1359+ GenericType errorType = errorTypes .get (String .valueOf (response .getStatus ()));
1360+ if (errorType == null ) {
1361+ errorType = errorTypes .get ("0" ); // "0" is the "default" response
1362+ }
1363+ try {
1364+ return deserialize (response , errorType );
1365+ } catch (Exception e ) {
1366+ return String .format ("Failed deserializing error entity: %s" , e .toString ());
1367+ }
1368+ }
13481369
13491370 protected Response sendRequest (String method , Invocation .Builder invocationBuilder , Entity <?> entity ) {
13501371 Response response ;
@@ -1371,7 +1392,7 @@ protected Response sendRequest(String method, Invocation.Builder invocationBuild
13711392 */
13721393 @ Deprecated
13731394 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 {
1374- return invokeAPI (null , path , method , queryParams , body , headerParams , cookieParams , formParams , accept , contentType , authNames , returnType , isBodyNullable );
1395+ return invokeAPI (null , path , method , queryParams , body , headerParams , cookieParams , formParams , accept , contentType , authNames , returnType , isBodyNullable , null /*TODO SME manage*/ );
13751396 }
13761397
13771398 /**
0 commit comments