Skip to content

Commit 19540ed

Browse files
committed
Store response headers and body in ApiException
1 parent a8c526e commit 19540ed

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public class ApiClient {
157157
}
158158
}
159159
catch (IOException e) {
160-
throw new ApiException(500, e.getMessage());
160+
throw new ApiException(500, e.getMessage(), null, json);
161161
}
162162
}
163163

@@ -260,17 +260,21 @@ public class ApiClient {
260260
}
261261
else {
262262
String message = "error";
263+
String respBody = null;
263264
if(response.hasEntity()) {
264265
try{
265-
message = String.valueOf(response.getEntity(String.class));
266+
respBody = String.valueOf(response.getEntity(String.class));
267+
message = respBody;
266268
}
267269
catch (RuntimeException e) {
268270
// e.printStackTrace();
269271
}
270272
}
271273
throw new ApiException(
272274
response.getClientResponseStatus().getStatusCode(),
273-
message);
275+
message,
276+
response.getHeaders(),
277+
respBody);
274278
}
275279
}
276280

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package {{invokerPackage}};
22

3+
import java.util.Map;
4+
import java.util.List;
5+
36
public class ApiException extends Exception {
4-
int code = 0;
5-
String message = null;
7+
private int code = 0;
8+
private String message = null;
9+
private Map<String, List<String>> responseHeaders = null;
10+
private String responseBody = null;
611
712
public ApiException() {}
813

@@ -11,19 +16,42 @@ public class ApiException extends Exception {
1116
this.message = message;
1217
}
1318

19+
public ApiException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
20+
this.code = code;
21+
this.message = message;
22+
this.responseHeaders = responseHeaders;
23+
this.responseBody = responseBody;
24+
}
25+
1426
public int getCode() {
1527
return code;
1628
}
17-
29+
1830
public void setCode(int code) {
1931
this.code = code;
2032
}
21-
33+
2234
public String getMessage() {
2335
return message;
2436
}
25-
37+
2638
public void setMessage(String message) {
2739
this.message = message;
2840
}
29-
}
41+
42+
public Map<String, List<String>> getResponseHeaders() {
43+
return responseHeaders;
44+
}
45+
46+
public void setResponseHeaders(Map<String, List<String>> responseHeaders) {
47+
this.responseHeaders = responseHeaders;
48+
}
49+
50+
public String getResponseBody() {
51+
return responseBody;
52+
}
53+
54+
public void setResponseBody(String responseBody) {
55+
this.responseBody = responseBody;
56+
}
57+
}

samples/client/petstore/java/src/main/java/io/swagger/client/ApiClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ else if(String.class.equals(cls)) {
157157
}
158158
}
159159
catch (IOException e) {
160-
throw new ApiException(500, e.getMessage());
160+
throw new ApiException(500, e.getMessage(), null, json);
161161
}
162162
}
163163

@@ -260,17 +260,21 @@ else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
260260
}
261261
else {
262262
String message = "error";
263+
String respBody = null;
263264
if(response.hasEntity()) {
264265
try{
265-
message = String.valueOf(response.getEntity(String.class));
266+
respBody = String.valueOf(response.getEntity(String.class));
267+
message = respBody;
266268
}
267269
catch (RuntimeException e) {
268270
// e.printStackTrace();
269271
}
270272
}
271273
throw new ApiException(
272274
response.getClientResponseStatus().getStatusCode(),
273-
message);
275+
message,
276+
response.getHeaders(),
277+
respBody);
274278
}
275279
}
276280

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package io.swagger.client;
22

3+
import java.util.Map;
4+
import java.util.List;
5+
36
public class ApiException extends Exception {
4-
int code = 0;
5-
String message = null;
7+
private int code = 0;
8+
private String message = null;
9+
private Map<String, List<String>> responseHeaders = null;
10+
private String responseBody = null;
611

712
public ApiException() {}
813

@@ -11,19 +16,42 @@ public ApiException(int code, String message) {
1116
this.message = message;
1217
}
1318

19+
public ApiException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
20+
this.code = code;
21+
this.message = message;
22+
this.responseHeaders = responseHeaders;
23+
this.responseBody = responseBody;
24+
}
25+
1426
public int getCode() {
1527
return code;
1628
}
17-
29+
1830
public void setCode(int code) {
1931
this.code = code;
2032
}
21-
33+
2234
public String getMessage() {
2335
return message;
2436
}
25-
37+
2638
public void setMessage(String message) {
2739
this.message = message;
2840
}
29-
}
41+
42+
public Map<String, List<String>> getResponseHeaders() {
43+
return responseHeaders;
44+
}
45+
46+
public void setResponseHeaders(Map<String, List<String>> responseHeaders) {
47+
this.responseHeaders = responseHeaders;
48+
}
49+
50+
public String getResponseBody() {
51+
return responseBody;
52+
}
53+
54+
public void setResponseBody(String responseBody) {
55+
this.responseBody = responseBody;
56+
}
57+
}

0 commit comments

Comments
 (0)