Skip to content

Commit

Permalink
[Java] [Native] Unify exception messages for async, add the status co…
Browse files Browse the repository at this point in the history
…de (#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 <jens.fischer@vier.ai>
  • Loading branch information
Gama11 and Gama11 authored Jul 5, 2021
1 parent a5c98dd commit 870c1e1
Show file tree
Hide file tree
Showing 20 changed files with 250 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,25 @@ public class {{classname}} {
}
{{#asyncNative}}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}
{{/asyncNative}}
{{^asyncNative}}

protected ApiException createApiException(HttpResponse<InputStream> response, String msgPrefix) throws IOException {
protected ApiException getApiException(String operationId, HttpResponse<InputStream> 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);
}
{{/asyncNative}}

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}}
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ public AnotherFakeApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}

protected ApiException createApiException(HttpResponse<InputStream> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,16 @@ public FakeApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}

protected ApiException createApiException(HttpResponse<InputStream> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ public FakeClassnameTags123Api(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}

protected ApiException createApiException(HttpResponse<InputStream> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,16 @@ public PetApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}

protected ApiException createApiException(HttpResponse<InputStream> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ public StoreApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}

protected ApiException createApiException(HttpResponse<InputStream> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ public UserApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

private ApiException getApiException(String operationId, HttpResponse<String>localVarResponse) {
return new ApiException(localVarResponse.statusCode(),
operationId + " call received non-success response",
localVarResponse.headers(),
localVarResponse.body());
private ApiException getApiException(String operationId, HttpResponse<String> response) {
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
return new ApiException(response.statusCode(), message, response.headers(), response.body());
}

protected ApiException createApiException(HttpResponse<InputStream> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ public AnotherFakeApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

protected ApiException createApiException(HttpResponse<InputStream> response, String msgPrefix) throws IOException {
protected ApiException getApiException(String operationId, HttpResponse<InputStream> 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;
}

/**
Expand Down Expand Up @@ -95,7 +100,7 @@ public ApiResponse<Client> call123testSpecialTagsWithHttpInfo(Client body) throw
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "call123testSpecialTags call received non-success response");
throw getApiException("call123testSpecialTags", localVarResponse);
}
return new ApiResponse<Client>(
localVarResponse.statusCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,17 @@ public FakeApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

protected ApiException createApiException(HttpResponse<InputStream> response, String msgPrefix) throws IOException {
protected ApiException getApiException(String operationId, HttpResponse<InputStream> 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;
}

/**
Expand Down Expand Up @@ -101,7 +106,7 @@ public ApiResponse<Void> createXmlItemWithHttpInfo(XmlItem xmlItem) throws ApiEx
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "createXmlItem call received non-success response");
throw getApiException("createXmlItem", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -175,7 +180,7 @@ public ApiResponse<Boolean> fakeOuterBooleanSerializeWithHttpInfo(Boolean body)
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "fakeOuterBooleanSerialize call received non-success response");
throw getApiException("fakeOuterBooleanSerialize", localVarResponse);
}
return new ApiResponse<Boolean>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -245,7 +250,7 @@ public ApiResponse<OuterComposite> fakeOuterCompositeSerializeWithHttpInfo(Outer
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "fakeOuterCompositeSerialize call received non-success response");
throw getApiException("fakeOuterCompositeSerialize", localVarResponse);
}
return new ApiResponse<OuterComposite>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -315,7 +320,7 @@ public ApiResponse<BigDecimal> fakeOuterNumberSerializeWithHttpInfo(BigDecimal b
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "fakeOuterNumberSerialize call received non-success response");
throw getApiException("fakeOuterNumberSerialize", localVarResponse);
}
return new ApiResponse<BigDecimal>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -385,7 +390,7 @@ public ApiResponse<String> fakeOuterStringSerializeWithHttpInfo(String body) thr
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "fakeOuterStringSerialize call received non-success response");
throw getApiException("fakeOuterStringSerialize", localVarResponse);
}
return new ApiResponse<String>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -453,7 +458,7 @@ public ApiResponse<Void> testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testBodyWithFileSchema call received non-success response");
throw getApiException("testBodyWithFileSchema", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -527,7 +532,7 @@ public ApiResponse<Void> testBodyWithQueryParamsWithHttpInfo(String query, User
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testBodyWithQueryParams call received non-success response");
throw getApiException("testBodyWithQueryParams", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -614,7 +619,7 @@ public ApiResponse<Client> testClientModelWithHttpInfo(Client body) throws ApiEx
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testClientModel call received non-success response");
throw getApiException("testClientModel", localVarResponse);
}
return new ApiResponse<Client>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -712,7 +717,7 @@ public ApiResponse<Void> testEndpointParametersWithHttpInfo(BigDecimal number, D
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testEndpointParameters call received non-success response");
throw getApiException("testEndpointParameters", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -804,7 +809,7 @@ public ApiResponse<Void> testEnumParametersWithHttpInfo(List<String> enumHeaderS
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testEnumParameters call received non-success response");
throw getApiException("testEnumParameters", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -927,7 +932,7 @@ public ApiResponse<Void> testGroupParametersWithHttpInfo(Integer requiredStringG
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testGroupParameters call received non-success response");
throw getApiException("testGroupParameters", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -1096,7 +1101,7 @@ public ApiResponse<Void> testInlineAdditionalPropertiesWithHttpInfo(Map<String,
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testInlineAdditionalProperties call received non-success response");
throw getApiException("testInlineAdditionalProperties", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -1170,7 +1175,7 @@ public ApiResponse<Void> testJsonFormDataWithHttpInfo(String param, String param
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testJsonFormData call received non-success response");
throw getApiException("testJsonFormData", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -1248,7 +1253,7 @@ public ApiResponse<Void> testQueryParameterCollectionFormatWithHttpInfo(List<Str
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw createApiException(localVarResponse, "testQueryParameterCollectionFormat call received non-success response");
throw getApiException("testQueryParameterCollectionFormat", localVarResponse);
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down
Loading

0 comments on commit 870c1e1

Please sign in to comment.