Skip to content

Commit 31735d0

Browse files
authored
fix(dynamic-error): removed side effects to keep error cases consistent (#91)
1 parent 3a10455 commit 31735d0

2 files changed

Lines changed: 72 additions & 75 deletions

File tree

src/main/java/io/apimatic/core/ErrorCase.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ private ErrorCase(final String reason, final ExceptionCreator<ExceptionType> exc
5959
* @throws ExceptionType Represents error response from the server.
6060
*/
6161
public void throwException(Context httpContext) throws ExceptionType {
62-
if (isErrorTemplate) {
63-
replacePlaceHolder(httpContext.getResponse());
64-
}
65-
throw exceptionCreator.apply(reason, httpContext);
62+
throw exceptionCreator.apply(getReason(httpContext.getResponse()), httpContext);
6663
}
6764

6865
/**
@@ -95,13 +92,33 @@ public static <ExceptionType extends CoreApiException> ErrorCase<ExceptionType>
9592
return new ErrorCase<ExceptionType>(reason, exceptionCreator, true);
9693
}
9794

98-
private void replacePlaceHolder(Response response) {
99-
replaceStatusCodeFromTemplate(response.getStatusCode());
100-
replaceHeadersFromTemplate(response.getHeaders());
101-
replaceBodyFromTemplate(response.getBody());
95+
private String getReason(Response response) {
96+
if (!isErrorTemplate) {
97+
return reason;
98+
}
99+
String resolvedReason = replaceStatusCodeFromTemplate(response.getStatusCode(), reason);
100+
resolvedReason = replaceHeadersFromTemplate(response.getHeaders(), resolvedReason);
101+
resolvedReason = replaceBodyFromTemplate(response.getBody(), resolvedReason);
102+
return resolvedReason;
103+
}
104+
105+
private String replaceStatusCodeFromTemplate(int statusCode, String reason) {
106+
StringBuilder formatter = new StringBuilder(reason);
107+
Matcher matcher = Pattern.compile("\\{(.*?)\\}").matcher(reason);
108+
while (matcher.find()) {
109+
String key = matcher.group(1);
110+
if (key.equals("$statusCode")) {
111+
String formatKey = String.format("{%s}", key);
112+
int index = formatter.indexOf(formatKey);
113+
if (index != -1) {
114+
formatter.replace(index, index + formatKey.length(), "" + statusCode);
115+
}
116+
}
117+
}
118+
return formatter.toString();
102119
}
103120

104-
private void replaceHeadersFromTemplate(HttpHeaders headers) {
121+
private String replaceHeadersFromTemplate(HttpHeaders headers, String reason) {
105122
StringBuilder formatter = new StringBuilder(reason);
106123
Matcher matcher = Pattern.compile("\\{(.*?)\\}").matcher(reason);
107124
while (matcher.find()) {
@@ -118,10 +135,10 @@ private void replaceHeadersFromTemplate(HttpHeaders headers) {
118135
}
119136
}
120137
}
121-
reason = formatter.toString();
138+
return formatter.toString();
122139
}
123140

124-
private void replaceBodyFromTemplate(String responseBody) {
141+
private String replaceBodyFromTemplate(String responseBody, String reason) {
125142
StringBuilder formatter = new StringBuilder(reason);
126143
JsonReader jsonReader = Json.createReader(new StringReader(responseBody));
127144
JsonStructure jsonStructure = null;
@@ -137,7 +154,7 @@ private void replaceBodyFromTemplate(String responseBody) {
137154
String pointerKey = key;
138155
replaceBodyString(responseBody, formatter, jsonStructure, key, pointerKey);
139156
}
140-
reason = formatter.toString().replaceAll("\"", "");
157+
return formatter.toString().replace("\"", "");
141158
}
142159

143160
private void replaceBodyString(String responseBody, StringBuilder formatter,
@@ -174,20 +191,4 @@ private String extractReplacementString(String responseBody, JsonStructure jsonS
174191
}
175192
return toReplaceString;
176193
}
177-
178-
private void replaceStatusCodeFromTemplate(int statusCode) {
179-
StringBuilder formatter = new StringBuilder(reason);
180-
Matcher matcher = Pattern.compile("\\{(.*?)\\}").matcher(reason);
181-
while (matcher.find()) {
182-
String key = matcher.group(1);
183-
if (key.equals("$statusCode")) {
184-
String formatKey = String.format("{%s}", key);
185-
int index = formatter.indexOf(formatKey);
186-
if (index != -1) {
187-
formatter.replace(index, index + formatKey.length(), "" + statusCode);
188-
}
189-
}
190-
}
191-
reason = formatter.toString();
192-
}
193194
}

src/test/java/apimatic/core/EndToEndTest.java

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,47 @@ public class EndToEndTest extends MockCoreConfig {
4848
private static final int INTERNAL_SERVER_ERROR = 500;
4949
private static final int GONE_CLIENT = 410;
5050

51+
/**
52+
* Map of Global Error Cases
53+
*/
54+
private static final Map<String, ErrorCase<CoreApiException>> GLOBAL_ERROR_CASES =
55+
new HashMap<String, ErrorCase<CoreApiException>>() {
56+
private static final long serialVersionUID = 1L;
57+
{
58+
put("400", ErrorCase.setTemplate(
59+
"Failed to make the request, {$statusCode} {$response.body#/errors/0/code} - "
60+
+ "{$response.body#/errors/0/detail}",
61+
(reason, context) -> new GlobalTestException(reason, context)));
62+
put("404", ErrorCase.setReason("Not found",
63+
(reason, context) -> new CoreApiException(reason, context)));
64+
put("401",
65+
ErrorCase.setTemplate("Failed to make the request, "
66+
+ "{$response.header.content-type} {$response.body#/errors/0/code}"
67+
+ " - {$response.body#/errors/0/detail}",
68+
(reason, context) -> new CoreApiException(reason, context)));
69+
put("410",
70+
ErrorCase.setTemplate("Failed to make the request, {$response.body}",
71+
(reason, context) -> new CoreApiException(reason, context)));
72+
put("405",
73+
ErrorCase.setTemplate("Failed to make the request, {$response.header.accept} "
74+
+ "{$response.body#/errors/0/code} - {$response.body#/errors/0/detail}",
75+
(reason, context) -> new CoreApiException(reason, context)));
76+
put("500",
77+
ErrorCase.setTemplate("Failed to make the request, http status code:"
78+
+ " {$statusCode}",
79+
(reason, context) -> new CoreApiException(reason, context)));
80+
put("4XX",
81+
ErrorCase.setTemplate(
82+
"Failed to make the request, {$response.body#/errors/0/code}"
83+
+ " - {$response.body#/errors/0/detail}",
84+
(reason, context) -> new CoreApiException(reason, context)));
85+
put(ErrorCase.DEFAULT,
86+
ErrorCase.setReason(
87+
"Failed to make the request, {$response.body#/errors/0/code}"
88+
+ " - {$response.body#/errors/0/detail}",
89+
(reason, context) -> new CoreApiException(reason, context)));
90+
}};
91+
5192
/**
5293
* Initializes mocks annotated with Mock.
5394
*/
@@ -418,7 +459,7 @@ private ApiCall<String, CoreApiException> getApiCallGlobalErrorTemplate(String r
418459
.httpMethod(Method.GET))
419460
.responseHandler(responseHandler -> responseHandler
420461
.deserializer(response -> CoreHelper.deserialize(response, String.class))
421-
.nullify404(false).globalErrorCase(getGlobalErrorCases()))
462+
.nullify404(false).globalErrorCase(GLOBAL_ERROR_CASES))
422463
.endpointConfiguration(
423464
param -> param.arraySerializationFormat(ArraySerializationFormat.INDEXED)
424465
.hasBinaryResponse(false).retryOption(RetryOption.DEFAULT))
@@ -445,7 +486,7 @@ private ApiCall<String, CoreApiException> getApiCallGlobalErrorTemplateWithHeade
445486
.httpMethod(Method.GET))
446487
.responseHandler(responseHandler -> responseHandler
447488
.deserializer(response -> CoreHelper.deserialize(response, String.class))
448-
.nullify404(false).globalErrorCase(getGlobalErrorCases()))
489+
.nullify404(false).globalErrorCase(GLOBAL_ERROR_CASES))
449490
.endpointConfiguration(
450491
param -> param.arraySerializationFormat(ArraySerializationFormat.INDEXED)
451492
.hasBinaryResponse(false).retryOption(RetryOption.DEFAULT))
@@ -479,49 +520,4 @@ private void prepareStub() throws IOException {
479520
when(context.getResponse()).thenReturn(response);
480521
when(response.getStatusCode()).thenReturn(SUCCESS_CODE);
481522
}
482-
483-
private Map<String, ErrorCase<CoreApiException>> getGlobalErrorCases() {
484-
Map<String, ErrorCase<CoreApiException>> globalErrorCase = new HashMap<>();
485-
globalErrorCase.put("400", ErrorCase.setTemplate(
486-
"Failed to make the request, {$statusCode} {$response.body#/errors/0/code} - "
487-
+ "{$response.body#/errors/0/detail}",
488-
(reason, context) -> new GlobalTestException(reason, context)));
489-
490-
globalErrorCase.put("404", ErrorCase.setReason("Not found",
491-
(reason, context) -> new CoreApiException(reason, context)));
492-
493-
globalErrorCase.put("401",
494-
ErrorCase.setTemplate("Failed to make the request, {$response.header.content-type} "
495-
+ "{$response.body#/errors/0/code} - {$response.body#/errors/0/detail}",
496-
(reason, context) -> new CoreApiException(reason, context)));
497-
498-
globalErrorCase.put("410",
499-
ErrorCase.setTemplate("Failed to make the request, {$response.body}",
500-
(reason, context) -> new CoreApiException(reason, context)));
501-
502-
globalErrorCase.put("405",
503-
ErrorCase.setTemplate("Failed to make the request, {$response.header.accept} "
504-
+ "{$response.body#/errors/0/code} - {$response.body#/errors/0/detail}",
505-
(reason, context) -> new CoreApiException(reason, context)));
506-
507-
globalErrorCase.put("500",
508-
ErrorCase.setTemplate("Failed to make the request, http status code: {$statusCode}",
509-
(reason, context) -> new CoreApiException(reason, context)));
510-
511-
globalErrorCase.put("4XX",
512-
ErrorCase.setTemplate(
513-
"Failed to make the request, {$response.body#/errors/0/code} -"
514-
+ " {$response.body#/errors/0/detail}",
515-
(reason, context) -> new CoreApiException(reason, context)));
516-
517-
globalErrorCase.put(ErrorCase.DEFAULT,
518-
ErrorCase.setReason(
519-
"Failed to make the request, {$response.body#/errors/0/code} - "
520-
+ "{$response.body#/errors/0/detail}",
521-
(reason, context) -> new CoreApiException(reason, context)));
522-
523-
return globalErrorCase;
524-
525-
}
526-
527523
}

0 commit comments

Comments
 (0)