6262import java .util .Arrays ;
6363import java .util .ArrayList ;
6464import java .util .Date ;
65+ import java .util .Locale ;
6566import java .util .stream .Collectors ;
6667import java .util .stream .Stream ;
6768import java .time .OffsetDateTime ;
@@ -974,6 +975,7 @@ public File prepareDownloadFile(Response response) throws IOException {
974975 * @param authNames The authentications to apply
975976 * @param returnType The return type into which to deserialize the response
976977 * @param isBodyNullable True if the body is nullable
978+ * @param errorTypes Mapping of error codes to types into which to deserialize the response
977979 * @return The response body in type of string
978980 * @throws ApiException API exception
979981 */
@@ -990,7 +992,9 @@ public <T> ApiResponse<T> invokeAPI(
990992 String contentType ,
991993 String [] authNames ,
992994 GenericType <T > returnType ,
993- boolean isBodyNullable )
995+ boolean isBodyNullable ,
996+ Map <String , GenericType > errorTypes
997+ )
994998 throws ApiException {
995999
9961000 String targetURL ;
@@ -1001,7 +1005,7 @@ public <T> ApiResponse<T> invokeAPI(
10011005 if (index < 0 || index >= serverConfigurations .size ()) {
10021006 throw new ArrayIndexOutOfBoundsException (
10031007 String .format (
1004- java . util . Locale .ROOT ,
1008+ Locale .ROOT ,
10051009 "Invalid index %d when selecting the host settings. Must be less than %d" ,
10061010 index , serverConfigurations .size ()));
10071011 }
@@ -1088,14 +1092,16 @@ public <T> ApiResponse<T> invokeAPI(
10881092 String respBody = null ;
10891093 if (response .hasEntity ()) {
10901094 try {
1095+ // call bufferEntity, so that a subsequent call to `readEntity` in `deserialize` doesn't fail
1096+ response .bufferEntity ();
10911097 respBody = String .valueOf (response .readEntity (String .class ));
10921098 message = respBody ;
10931099 } catch (RuntimeException e ) {
10941100 // e.printStackTrace();
10951101 }
10961102 }
10971103 throw new ApiException (
1098- response .getStatus (), message , buildResponseHeaders (response ), respBody );
1104+ response .getStatus (), message , buildResponseHeaders (response ), respBody , deserializeErrorEntity ( errorTypes , response ) );
10991105 }
11001106 } finally {
11011107 try {
@@ -1106,6 +1112,21 @@ public <T> ApiResponse<T> invokeAPI(
11061112 }
11071113 }
11081114 }
1115+
1116+ private Object deserializeErrorEntity (Map <String , GenericType > errorTypes , Response response ) {
1117+ if (errorTypes == null ) {
1118+ return null ;
1119+ }
1120+ GenericType errorType = errorTypes .get (String .valueOf (response .getStatus ()));
1121+ if (errorType == null ) {
1122+ errorType = errorTypes .get ("0" ); // "0" is the "default" response
1123+ }
1124+ try {
1125+ return deserialize (response , errorType );
1126+ } catch (Exception e ) {
1127+ return null ;
1128+ }
1129+ }
11091130
11101131 protected Response sendRequest (String method , Invocation .Builder invocationBuilder , Entity <?> entity ) {
11111132 Response response ;
@@ -1132,7 +1153,7 @@ protected Response sendRequest(String method, Invocation.Builder invocationBuild
11321153 */
11331154 @ Deprecated
11341155 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 {
1135- return invokeAPI (null , path , method , queryParams , body , headerParams , cookieParams , formParams , accept , contentType , authNames , returnType , isBodyNullable );
1156+ return invokeAPI (null , path , method , queryParams , body , headerParams , cookieParams , formParams , accept , contentType , authNames , returnType , isBodyNullable , null /*TODO SME manage*/ );
11361157 }
11371158
11381159 /**
0 commit comments