From 870c1e1894229acecc3e313ab35d437ab4675a07 Mon Sep 17 00:00:00 2001 From: Jens Fischer Date: Mon, 5 Jul 2021 02:57:40 +0200 Subject: [PATCH] [Java] [Native] Unify exception messages for async, add the status code (#9825) * [Java] [Native] Unify exception messages for async, add the status code The template has two methods for creating API exceptions, and the enhancements from #9169 didn't make it to the async version. - unify the signatures of the two methods (name, arguments) - make sure the sync version is not generated with asyncNative - extract the formatting logic into a common formatExceptionMessage() method - add the status code to the exception message as well, not just the body - shortened "call received non-success response" to a more concise "call failed with" * Treat an empty body the same as a null body Co-authored-by: Jens Fischer --- .../Java/libraries/native/api.mustache | 27 +++++++----- .../client/api/AnotherFakeApi.java | 17 +++----- .../org/openapitools/client/api/FakeApi.java | 17 +++----- .../client/api/FakeClassnameTags123Api.java | 17 +++----- .../org/openapitools/client/api/PetApi.java | 17 +++----- .../org/openapitools/client/api/StoreApi.java | 17 +++----- .../org/openapitools/client/api/UserApi.java | 17 +++----- .../client/api/AnotherFakeApi.java | 15 ++++--- .../org/openapitools/client/api/FakeApi.java | 41 ++++++++++-------- .../client/api/FakeClassnameTags123Api.java | 15 ++++--- .../org/openapitools/client/api/PetApi.java | 31 +++++++------ .../org/openapitools/client/api/StoreApi.java | 21 +++++---- .../org/openapitools/client/api/UserApi.java | 29 +++++++------ .../client/api/AnotherFakeApi.java | 15 ++++--- .../openapitools/client/api/DefaultApi.java | 15 ++++--- .../org/openapitools/client/api/FakeApi.java | 43 +++++++++++-------- .../client/api/FakeClassnameTags123Api.java | 15 ++++--- .../org/openapitools/client/api/PetApi.java | 31 +++++++------ .../org/openapitools/client/api/StoreApi.java | 21 +++++---- .../org/openapitools/client/api/UserApi.java | 29 +++++++------ 20 files changed, 251 insertions(+), 199 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache index e91e72f97b9..35922945360 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache @@ -57,20 +57,25 @@ public class {{classname}} { } {{#asyncNative}} - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); + } + {{/asyncNative}} + {{^asyncNative}} + + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { + String body = response.body() == null ? null : new String(response.body().readAllBytes()); + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); } {{/asyncNative}} - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } {{#operation}} @@ -215,7 +220,7 @@ public class {{classname}} { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "{{operationId}} call received non-success response"); + throw getApiException("{{operationId}}", localVarResponse); } return new ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>( localVarResponse.statusCode(), diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index f2247d141bb..59947067502 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -60,19 +60,16 @@ public class AnotherFakeApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java index 388dce55a87..c84924f6aba 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeApi.java @@ -68,19 +68,16 @@ public class FakeApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index f8076cd6914..ea9a7d97193 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -60,19 +60,16 @@ public class FakeClassnameTags123Api { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java index 2dcd304b1f5..e65cb647aaf 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/PetApi.java @@ -63,19 +63,16 @@ public class PetApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java index ec19468ccef..82bb7296d85 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/StoreApi.java @@ -60,19 +60,16 @@ public class StoreApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** diff --git a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java index ba2cad99cf8..3d9893a34f4 100644 --- a/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/native-async/src/main/java/org/openapitools/client/api/UserApi.java @@ -60,19 +60,16 @@ public class UserApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - private ApiException getApiException(String operationId, HttpResponselocalVarResponse) { - return new ApiException(localVarResponse.statusCode(), - operationId + " call received non-success response", - localVarResponse.headers(), - localVarResponse.body()); + private ApiException getApiException(String operationId, HttpResponse response) { + String message = formatExceptionMessage(operationId, response.statusCode(), response.body()); + return new ApiException(response.statusCode(), message, response.headers(), response.body()); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { - String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index 2c0088e76d8..e31f3cd51f9 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -58,12 +58,17 @@ public class AnotherFakeApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -95,7 +100,7 @@ public class AnotherFakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "call123testSpecialTags call received non-success response"); + throw getApiException("call123testSpecialTags", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java index 6d3b64d8344..d12c79a0c0d 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java @@ -66,12 +66,17 @@ public class FakeApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -101,7 +106,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createXmlItem call received non-success response"); + throw getApiException("createXmlItem", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -175,7 +180,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterBooleanSerialize call received non-success response"); + throw getApiException("fakeOuterBooleanSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -245,7 +250,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterCompositeSerialize call received non-success response"); + throw getApiException("fakeOuterCompositeSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -315,7 +320,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterNumberSerialize call received non-success response"); + throw getApiException("fakeOuterNumberSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -385,7 +390,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterStringSerialize call received non-success response"); + throw getApiException("fakeOuterStringSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -453,7 +458,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testBodyWithFileSchema call received non-success response"); + throw getApiException("testBodyWithFileSchema", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -527,7 +532,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testBodyWithQueryParams call received non-success response"); + throw getApiException("testBodyWithQueryParams", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -614,7 +619,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testClientModel call received non-success response"); + throw getApiException("testClientModel", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -712,7 +717,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testEndpointParameters call received non-success response"); + throw getApiException("testEndpointParameters", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -804,7 +809,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testEnumParameters call received non-success response"); + throw getApiException("testEnumParameters", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -927,7 +932,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testGroupParameters call received non-success response"); + throw getApiException("testGroupParameters", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -1096,7 +1101,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testInlineAdditionalProperties call received non-success response"); + throw getApiException("testInlineAdditionalProperties", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -1170,7 +1175,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testJsonFormData call received non-success response"); + throw getApiException("testJsonFormData", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -1248,7 +1253,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testQueryParameterCollectionFormat call received non-success response"); + throw getApiException("testQueryParameterCollectionFormat", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index b16a097a9c3..f81bfe6b042 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -58,12 +58,17 @@ public class FakeClassnameTags123Api { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -95,7 +100,7 @@ public class FakeClassnameTags123Api { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testClassname call received non-success response"); + throw getApiException("testClassname", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java index ff744b27275..36cdfe2419d 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java @@ -61,12 +61,17 @@ public class PetApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -96,7 +101,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "addPet call received non-success response"); + throw getApiException("addPet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -170,7 +175,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "deletePet call received non-success response"); + throw getApiException("deletePet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -242,7 +247,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "findPetsByStatus call received non-success response"); + throw getApiException("findPetsByStatus", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -323,7 +328,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "findPetsByTags call received non-success response"); + throw getApiException("findPetsByTags", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -400,7 +405,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getPetById call received non-success response"); + throw getApiException("getPetById", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -467,7 +472,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "updatePet call received non-success response"); + throw getApiException("updatePet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -543,7 +548,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "updatePetWithForm call received non-success response"); + throw getApiException("updatePetWithForm", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -616,7 +621,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "uploadFile call received non-success response"); + throw getApiException("uploadFile", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -689,7 +694,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "uploadFileWithRequiredFile call received non-success response"); + throw getApiException("uploadFileWithRequiredFile", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java index caffb9cd6ff..7ff31d54a16 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java @@ -58,12 +58,17 @@ public class StoreApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -93,7 +98,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "deleteOrder call received non-success response"); + throw getApiException("deleteOrder", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -160,7 +165,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getInventory call received non-success response"); + throw getApiException("getInventory", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -224,7 +229,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getOrderById call received non-success response"); + throw getApiException("getOrderById", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -293,7 +298,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "placeOrder call received non-success response"); + throw getApiException("placeOrder", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java index e841c2cd1e6..3d657dacaea 100644 --- a/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java @@ -58,12 +58,17 @@ public class UserApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -93,7 +98,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createUser call received non-success response"); + throw getApiException("createUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -165,7 +170,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createUsersWithArrayInput call received non-success response"); + throw getApiException("createUsersWithArrayInput", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -237,7 +242,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createUsersWithListInput call received non-success response"); + throw getApiException("createUsersWithListInput", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -309,7 +314,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "deleteUser call received non-success response"); + throw getApiException("deleteUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -378,7 +383,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getUserByName call received non-success response"); + throw getApiException("getUserByName", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -449,7 +454,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "loginUser call received non-success response"); + throw getApiException("loginUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -527,7 +532,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "logoutUser call received non-success response"); + throw getApiException("logoutUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -591,7 +596,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "updateUser call received non-success response"); + throw getApiException("updateUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index 1d87022e718..9e3aae36505 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -58,12 +58,17 @@ public class AnotherFakeApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -95,7 +100,7 @@ public class AnotherFakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "call123testSpecialTags call received non-success response"); + throw getApiException("call123testSpecialTags", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java index fc06f1fc039..5dc086e5edb 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/DefaultApi.java @@ -58,12 +58,17 @@ public class DefaultApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -93,7 +98,7 @@ public class DefaultApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fooGet call received non-success response"); + throw getApiException("fooGet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java index b5b0a141d33..996001b7aac 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeApi.java @@ -67,12 +67,17 @@ public class FakeApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -102,7 +107,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeHealthGet call received non-success response"); + throw getApiException("fakeHealthGet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -166,7 +171,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterBooleanSerialize call received non-success response"); + throw getApiException("fakeOuterBooleanSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -236,7 +241,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterCompositeSerialize call received non-success response"); + throw getApiException("fakeOuterCompositeSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -306,7 +311,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterNumberSerialize call received non-success response"); + throw getApiException("fakeOuterNumberSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -376,7 +381,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "fakeOuterStringSerialize call received non-success response"); + throw getApiException("fakeOuterStringSerialize", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -444,7 +449,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getArrayOfEnums call received non-success response"); + throw getApiException("getArrayOfEnums", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -506,7 +511,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testBodyWithFileSchema call received non-success response"); + throw getApiException("testBodyWithFileSchema", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -580,7 +585,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testBodyWithQueryParams call received non-success response"); + throw getApiException("testBodyWithQueryParams", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -667,7 +672,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testClientModel call received non-success response"); + throw getApiException("testClientModel", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -765,7 +770,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testEndpointParameters call received non-success response"); + throw getApiException("testEndpointParameters", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -857,7 +862,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testEnumParameters call received non-success response"); + throw getApiException("testEnumParameters", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -980,7 +985,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testGroupParameters call received non-success response"); + throw getApiException("testGroupParameters", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -1149,7 +1154,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testInlineAdditionalProperties call received non-success response"); + throw getApiException("testInlineAdditionalProperties", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -1223,7 +1228,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testJsonFormData call received non-success response"); + throw getApiException("testJsonFormData", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -1301,7 +1306,7 @@ public class FakeApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testQueryParameterCollectionFormat call received non-success response"); + throw getApiException("testQueryParameterCollectionFormat", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index caeaf872f78..098b07e149e 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -58,12 +58,17 @@ public class FakeClassnameTags123Api { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -95,7 +100,7 @@ public class FakeClassnameTags123Api { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "testClassname call received non-success response"); + throw getApiException("testClassname", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java index 530d75960d9..4f2bfe172fe 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/PetApi.java @@ -60,12 +60,17 @@ public class PetApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -95,7 +100,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "addPet call received non-success response"); + throw getApiException("addPet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -169,7 +174,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "deletePet call received non-success response"); + throw getApiException("deletePet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -241,7 +246,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "findPetsByStatus call received non-success response"); + throw getApiException("findPetsByStatus", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -322,7 +327,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "findPetsByTags call received non-success response"); + throw getApiException("findPetsByTags", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -399,7 +404,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getPetById call received non-success response"); + throw getApiException("getPetById", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -466,7 +471,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "updatePet call received non-success response"); + throw getApiException("updatePet", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -542,7 +547,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "updatePetWithForm call received non-success response"); + throw getApiException("updatePetWithForm", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -615,7 +620,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "uploadFile call received non-success response"); + throw getApiException("uploadFile", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -688,7 +693,7 @@ public class PetApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "uploadFileWithRequiredFile call received non-success response"); + throw getApiException("uploadFileWithRequiredFile", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java index c06efabb4a8..0bd1cd35ff4 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/StoreApi.java @@ -58,12 +58,17 @@ public class StoreApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -93,7 +98,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "deleteOrder call received non-success response"); + throw getApiException("deleteOrder", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -160,7 +165,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getInventory call received non-success response"); + throw getApiException("getInventory", localVarResponse); } return new ApiResponse>( localVarResponse.statusCode(), @@ -224,7 +229,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getOrderById call received non-success response"); + throw getApiException("getOrderById", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -293,7 +298,7 @@ public class StoreApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "placeOrder call received non-success response"); + throw getApiException("placeOrder", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), diff --git a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java index 3c1a68c9881..008303b316d 100644 --- a/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/openapi3/client/petstore/java/native/src/main/java/org/openapitools/client/api/UserApi.java @@ -58,12 +58,17 @@ public class UserApi { memberVarResponseInterceptor = apiClient.getResponseInterceptor(); } - protected ApiException createApiException(HttpResponse response, String msgPrefix) throws IOException { + protected ApiException getApiException(String operationId, HttpResponse response) throws IOException { String body = response.body() == null ? null : new String(response.body().readAllBytes()); - if (body != null) { - msgPrefix += ": " + body; + String message = formatExceptionMessage(operationId, response.statusCode(), body); + return new ApiException(response.statusCode(), message, response.headers(), body); + } + + private String formatExceptionMessage(String operationId, int statusCode, String body) { + if (body == null || body.isEmpty()) { + body = "[no body]"; } - return new ApiException(response.statusCode(), msgPrefix, response.headers(), body); + return operationId + " call failed with: " + statusCode + " - " + body; } /** @@ -93,7 +98,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createUser call received non-success response"); + throw getApiException("createUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -165,7 +170,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createUsersWithArrayInput call received non-success response"); + throw getApiException("createUsersWithArrayInput", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -237,7 +242,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "createUsersWithListInput call received non-success response"); + throw getApiException("createUsersWithListInput", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -309,7 +314,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "deleteUser call received non-success response"); + throw getApiException("deleteUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -378,7 +383,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "getUserByName call received non-success response"); + throw getApiException("getUserByName", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -449,7 +454,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "loginUser call received non-success response"); + throw getApiException("loginUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -527,7 +532,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "logoutUser call received non-success response"); + throw getApiException("logoutUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(), @@ -591,7 +596,7 @@ public class UserApi { memberVarResponseInterceptor.accept(localVarResponse); } if (localVarResponse.statusCode()/ 100 != 2) { - throw createApiException(localVarResponse, "updateUser call received non-success response"); + throw getApiException("updateUser", localVarResponse); } return new ApiResponse( localVarResponse.statusCode(),