[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 <jens.fischer@vier.ai>
This commit is contained in:
Jens Fischer
2021-07-05 02:57:40 +02:00
committed by GitHub
parent a5c98dd416
commit 870c1e1894
20 changed files with 251 additions and 199 deletions

View File

@@ -60,19 +60,16 @@ public class AnotherFakeApi {
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;
}
/**

View File

@@ -68,19 +68,16 @@ public class FakeApi {
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;
}
/**

View File

@@ -60,19 +60,16 @@ public class FakeClassnameTags123Api {
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;
}
/**

View File

@@ -63,19 +63,16 @@ public class PetApi {
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;
}
/**

View File

@@ -60,19 +60,16 @@ public class StoreApi {
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;
}
/**

View File

@@ -60,19 +60,16 @@ public class UserApi {
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;
}
/**