Skip to content

Commit

Permalink
[Java] [Native] Add response body to exception message (#9169)
Browse files Browse the repository at this point in the history
* Fix #8027 - import the auto generated supporting JSON class only when generateSupportingFiles is true

* Fix #8027 - import the auto generated supporting JSON class only when generateSupportingFiles is true

* Fix #8027 - import the auto generated supporting JSON class only when generateSupportingFiles is true

* Revert "Fix #8027 - import the auto generated supporting JSON class only when generateSupportingFiles is true"

This reverts commit 56e2b1f

* Revert "Fix #8027 - import the auto generated supporting JSON class only when generateSupportingFiles is true"

This reverts commit 335c304

* Fix #8027 - import the auto generated supporting JSON class only when generateSupportingFiles is true

* Fix #8027 - import the auto generated supporting JSON class only when discriminator is needed

* Fix #8027 - import the auto generated supporting JSON class only when discriminator is needed

* Fix #8027 - import the auto generated supporting JSON class only when discriminator is needed

* [Java] [Native] Add response body to exception message

* [Java] [Native] Use default base URI if baseUri param is null

* [Java] [Native] Use default base URI if baseUri param is null
  • Loading branch information
MosheElisha authored Apr 16, 2021
1 parent 6dce817 commit af992e4
Show file tree
Hide file tree
Showing 24 changed files with 241 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public class ApiClient {
public ApiClient(HttpClient.Builder builder, ObjectMapper mapper, String baseUri) {
this.builder = builder;
this.mapper = mapper;
updateBaseUri(baseUri);
updateBaseUri(baseUri != null ? baseUri : getDefaultBaseUri());
interceptor = null;
readTimeout = null;
responseInterceptor = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public class {{classname}} {
}
{{/asyncNative}}

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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

{{#operation}}
{{#vendorExtensions.x-group-parameters}}
{{#hasParams}}
Expand Down Expand Up @@ -207,10 +215,7 @@ public class {{classname}} {
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"{{operationId}} call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "{{operationId}} call received non-success response");
}
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 @@ -175,7 +175,7 @@ public ApiClient() {
public ApiClient(HttpClient.Builder builder, ObjectMapper mapper, String baseUri) {
this.builder = builder;
this.mapper = mapper;
updateBaseUri(baseUri);
updateBaseUri(baseUri != null ? baseUri : getDefaultBaseUri());
interceptor = null;
readTimeout = null;
responseInterceptor = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ private ApiException getApiException(String operationId, HttpResponse<String>loc
localVarResponse.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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* To test special tags
* To test special tags and operation ID starting with number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ private ApiException getApiException(String operationId, HttpResponse<String>loc
localVarResponse.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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* creates an XmlItem
* this route creates an XmlItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ private ApiException getApiException(String operationId, HttpResponse<String>loc
localVarResponse.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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* To test class name in snake case
* To test class name in snake case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ private ApiException getApiException(String operationId, HttpResponse<String>loc
localVarResponse.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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* Add a new pet to the store
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ private ApiException getApiException(String operationId, HttpResponse<String>loc
localVarResponse.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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* Delete purchase order by ID
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ private ApiException getApiException(String operationId, HttpResponse<String>loc
localVarResponse.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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* Create user
* This can only be done by the logged in user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public ApiClient() {
public ApiClient(HttpClient.Builder builder, ObjectMapper mapper, String baseUri) {
this.builder = builder;
this.mapper = mapper;
updateBaseUri(baseUri);
updateBaseUri(baseUri != null ? baseUri : getDefaultBaseUri());
interceptor = null;
readTimeout = null;
responseInterceptor = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public AnotherFakeApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* To test special tags
* To test special tags and operation ID starting with number
Expand Down Expand Up @@ -87,10 +95,7 @@ public ApiResponse<Client> call123testSpecialTagsWithHttpInfo(Client body) throw
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"call123testSpecialTags call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "call123testSpecialTags call received non-success response");
}
return new ApiResponse<Client>(
localVarResponse.statusCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public FakeApi(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* creates an XmlItem
* this route creates an XmlItem
Expand Down Expand Up @@ -93,10 +101,7 @@ public ApiResponse<Void> createXmlItemWithHttpInfo(XmlItem xmlItem) throws ApiEx
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"createXmlItem call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "createXmlItem call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -170,10 +175,7 @@ public ApiResponse<Boolean> fakeOuterBooleanSerializeWithHttpInfo(Boolean body)
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"fakeOuterBooleanSerialize call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "fakeOuterBooleanSerialize call received non-success response");
}
return new ApiResponse<Boolean>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -243,10 +245,7 @@ public ApiResponse<OuterComposite> fakeOuterCompositeSerializeWithHttpInfo(Outer
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"fakeOuterCompositeSerialize call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "fakeOuterCompositeSerialize call received non-success response");
}
return new ApiResponse<OuterComposite>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -316,10 +315,7 @@ public ApiResponse<BigDecimal> fakeOuterNumberSerializeWithHttpInfo(BigDecimal b
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"fakeOuterNumberSerialize call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "fakeOuterNumberSerialize call received non-success response");
}
return new ApiResponse<BigDecimal>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -389,10 +385,7 @@ public ApiResponse<String> fakeOuterStringSerializeWithHttpInfo(String body) thr
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"fakeOuterStringSerialize call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "fakeOuterStringSerialize call received non-success response");
}
return new ApiResponse<String>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -460,10 +453,7 @@ public ApiResponse<Void> testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testBodyWithFileSchema call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testBodyWithFileSchema call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -537,10 +527,7 @@ public ApiResponse<Void> testBodyWithQueryParamsWithHttpInfo(String query, User
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testBodyWithQueryParams call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testBodyWithQueryParams call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -627,10 +614,7 @@ public ApiResponse<Client> testClientModelWithHttpInfo(Client body) throws ApiEx
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testClientModel call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testClientModel call received non-success response");
}
return new ApiResponse<Client>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -728,10 +712,7 @@ public ApiResponse<Void> testEndpointParametersWithHttpInfo(BigDecimal number, D
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testEndpointParameters call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testEndpointParameters call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -823,10 +804,7 @@ public ApiResponse<Void> testEnumParametersWithHttpInfo(List<String> enumHeaderS
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testEnumParameters call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testEnumParameters call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -949,10 +927,7 @@ public ApiResponse<Void> testGroupParametersWithHttpInfo(Integer requiredStringG
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testGroupParameters call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testGroupParameters call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -1121,10 +1096,7 @@ public ApiResponse<Void> testInlineAdditionalPropertiesWithHttpInfo(Map<String,
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testInlineAdditionalProperties call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testInlineAdditionalProperties call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -1198,10 +1170,7 @@ public ApiResponse<Void> testJsonFormDataWithHttpInfo(String param, String param
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testJsonFormData call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testJsonFormData call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down Expand Up @@ -1279,10 +1248,7 @@ public ApiResponse<Void> testQueryParameterCollectionFormatWithHttpInfo(List<Str
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testQueryParameterCollectionFormat call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testQueryParameterCollectionFormat call received non-success response");
}
return new ApiResponse<Void>(
localVarResponse.statusCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public FakeClassnameTags123Api(ApiClient apiClient) {
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
}

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;
}
return new ApiException(response.statusCode(), msgPrefix, response.headers(), body);
}

/**
* To test class name in snake case
* To test class name in snake case
Expand Down Expand Up @@ -87,10 +95,7 @@ public ApiResponse<Client> testClassnameWithHttpInfo(Client body) throws ApiExce
memberVarResponseInterceptor.accept(localVarResponse);
}
if (localVarResponse.statusCode()/ 100 != 2) {
throw new ApiException(localVarResponse.statusCode(),
"testClassname call received non-success response",
localVarResponse.headers(),
localVarResponse.body() == null ? null : new String(localVarResponse.body().readAllBytes()));
throw createApiException(localVarResponse, "testClassname call received non-success response");
}
return new ApiResponse<Client>(
localVarResponse.statusCode(),
Expand Down
Loading

0 comments on commit af992e4

Please sign in to comment.