Skip to content

Commit bd31b18

Browse files
author
Fernando Saint-Jean
committed
updates formatting according to java style and removing one of the java 8's from the builds
1 parent 2b09738 commit bd31b18

File tree

3 files changed

+220
-200
lines changed

3 files changed

+220
-200
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cache:
2424
directories:
2525
- $HOME/.m2
2626

27+
2728
matrix:
2829
include:
2930
- os: linux
@@ -32,8 +33,9 @@ matrix:
3233
apt:
3334
packages:
3435
- oracle-java8-installer
35-
- os: linux
36-
jdk: openjdk8
36+
#removing openjdk8 from matrix for now as it causes issues on release
37+
# - os: linux
38+
# jdk: openjdk8
3739
- os: linux
3840
jdk: openjdk11
3941

src/main/java/feign/error/AnnotationErrorDecoder.java

Lines changed: 121 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -16,145 +16,155 @@
1616
import feign.Response;
1717
import feign.codec.Decoder;
1818
import feign.codec.ErrorDecoder;
19-
2019
import java.lang.reflect.Method;
2120
import java.util.HashMap;
2221
import java.util.Map;
2322
import java.util.Optional;
24-
2523
import static feign.Feign.configKey;
2624

2725
public class AnnotationErrorDecoder implements ErrorDecoder {
2826

29-
private final Map<String, MethodErrorHandler> errorHandlerMap;
30-
private final ErrorDecoder defaultDecoder;
27+
private final Map<String, MethodErrorHandler> errorHandlerMap;
28+
private final ErrorDecoder defaultDecoder;
3129

3230

33-
AnnotationErrorDecoder(Map<String, MethodErrorHandler> errorHandlerMap, ErrorDecoder defaultDecoder) {
34-
this.errorHandlerMap = errorHandlerMap;
35-
this.defaultDecoder = defaultDecoder;
36-
}
31+
AnnotationErrorDecoder(Map<String, MethodErrorHandler> errorHandlerMap,
32+
ErrorDecoder defaultDecoder) {
33+
this.errorHandlerMap = errorHandlerMap;
34+
this.defaultDecoder = defaultDecoder;
35+
}
3736

38-
@Override
39-
public Exception decode(String methodKey, Response response) {
40-
if(errorHandlerMap.containsKey(methodKey)) {
41-
return errorHandlerMap.get(methodKey).decode(response);
42-
}
43-
return defaultDecoder.decode(methodKey, response);
37+
@Override
38+
public Exception decode(String methodKey, Response response) {
39+
if (errorHandlerMap.containsKey(methodKey)) {
40+
return errorHandlerMap.get(methodKey).decode(response);
4441
}
42+
return defaultDecoder.decode(methodKey, response);
43+
}
4544

4645

47-
public static AnnotationErrorDecoder.Builder builderFor(Class<?> apiType) {
48-
return new Builder(apiType);
49-
}
46+
public static AnnotationErrorDecoder.Builder builderFor(Class<?> apiType) {
47+
return new Builder(apiType);
48+
}
5049

51-
public static class Builder {
52-
private final Class<?> apiType;
53-
private ErrorDecoder defaultDecoder = new ErrorDecoder.Default();
54-
private Decoder responseBodyDecoder = new Decoder.Default();
50+
public static class Builder {
51+
private final Class<?> apiType;
52+
private ErrorDecoder defaultDecoder = new ErrorDecoder.Default();
53+
private Decoder responseBodyDecoder = new Decoder.Default();
5554

5655

57-
public Builder(Class<?> apiType) {
58-
this.apiType = apiType;
59-
}
56+
public Builder(Class<?> apiType) {
57+
this.apiType = apiType;
58+
}
6059

61-
public Builder withDefaultDecoder(ErrorDecoder defaultDecoder) {
62-
this.defaultDecoder = defaultDecoder;
63-
return this;
64-
}
60+
public Builder withDefaultDecoder(ErrorDecoder defaultDecoder) {
61+
this.defaultDecoder = defaultDecoder;
62+
return this;
63+
}
6564

66-
public Builder withResponseBodyDecoder(Decoder responseBodyDecoder) {
67-
this.responseBodyDecoder = responseBodyDecoder;
68-
return this;
69-
}
65+
public Builder withResponseBodyDecoder(Decoder responseBodyDecoder) {
66+
this.responseBodyDecoder = responseBodyDecoder;
67+
return this;
68+
}
7069

71-
public AnnotationErrorDecoder build() {
72-
Map<String, MethodErrorHandler> errorHandlerMap = generateErrorHandlerMapFromApi(apiType);
73-
return new AnnotationErrorDecoder(errorHandlerMap, defaultDecoder);
74-
}
70+
public AnnotationErrorDecoder build() {
71+
Map<String, MethodErrorHandler> errorHandlerMap = generateErrorHandlerMapFromApi(apiType);
72+
return new AnnotationErrorDecoder(errorHandlerMap, defaultDecoder);
73+
}
7574

76-
Map<String, MethodErrorHandler> generateErrorHandlerMapFromApi(Class<?> apiType) {
77-
78-
ExceptionGenerator classLevelDefault = new ExceptionGenerator.Builder()
79-
.withResponseBodyDecoder(responseBodyDecoder)
80-
.withExceptionType(ErrorHandling.NO_DEFAULT.class)
81-
.build();
82-
Map<Integer, ExceptionGenerator> classLevelStatusCodeDefinitions = new HashMap<Integer, ExceptionGenerator>();
83-
84-
Optional<ErrorHandling> classLevelErrorHandling = readErrorHandlingIncludingInherited(apiType);
85-
if(classLevelErrorHandling.isPresent()) {
86-
ErrorHandlingDefinition classErrorHandlingDefinition = readAnnotation(classLevelErrorHandling.get(), responseBodyDecoder);
87-
classLevelDefault = classErrorHandlingDefinition.defaultThrow;
88-
classLevelStatusCodeDefinitions = classErrorHandlingDefinition.statusCodesMap;
89-
}
90-
91-
Map<String, MethodErrorHandler> methodErrorHandlerMap = new HashMap<String, MethodErrorHandler>();
92-
for(Method method : apiType.getMethods()) {
93-
if(method.isAnnotationPresent(ErrorHandling.class)) {
94-
ErrorHandlingDefinition methodErrorHandling = readAnnotation(method.getAnnotation(ErrorHandling.class), responseBodyDecoder);
95-
ExceptionGenerator methodDefault = methodErrorHandling.defaultThrow;
96-
if(methodDefault.getExceptionType().equals(ErrorHandling.NO_DEFAULT.class)) {
97-
methodDefault = classLevelDefault;
98-
}
99-
100-
MethodErrorHandler methodErrorHandler =
101-
new MethodErrorHandler(methodErrorHandling.statusCodesMap, classLevelStatusCodeDefinitions, methodDefault );
102-
103-
methodErrorHandlerMap.put(configKey(apiType, method), methodErrorHandler);
104-
}
105-
}
106-
107-
return methodErrorHandlerMap;
75+
Map<String, MethodErrorHandler> generateErrorHandlerMapFromApi(Class<?> apiType) {
76+
77+
ExceptionGenerator classLevelDefault = new ExceptionGenerator.Builder()
78+
.withResponseBodyDecoder(responseBodyDecoder)
79+
.withExceptionType(ErrorHandling.NO_DEFAULT.class)
80+
.build();
81+
Map<Integer, ExceptionGenerator> classLevelStatusCodeDefinitions =
82+
new HashMap<Integer, ExceptionGenerator>();
83+
84+
Optional<ErrorHandling> classLevelErrorHandling =
85+
readErrorHandlingIncludingInherited(apiType);
86+
if (classLevelErrorHandling.isPresent()) {
87+
ErrorHandlingDefinition classErrorHandlingDefinition =
88+
readAnnotation(classLevelErrorHandling.get(), responseBodyDecoder);
89+
classLevelDefault = classErrorHandlingDefinition.defaultThrow;
90+
classLevelStatusCodeDefinitions = classErrorHandlingDefinition.statusCodesMap;
91+
}
92+
93+
Map<String, MethodErrorHandler> methodErrorHandlerMap =
94+
new HashMap<String, MethodErrorHandler>();
95+
for (Method method : apiType.getMethods()) {
96+
if (method.isAnnotationPresent(ErrorHandling.class)) {
97+
ErrorHandlingDefinition methodErrorHandling =
98+
readAnnotation(method.getAnnotation(ErrorHandling.class), responseBodyDecoder);
99+
ExceptionGenerator methodDefault = methodErrorHandling.defaultThrow;
100+
if (methodDefault.getExceptionType().equals(ErrorHandling.NO_DEFAULT.class)) {
101+
methodDefault = classLevelDefault;
102+
}
103+
104+
MethodErrorHandler methodErrorHandler =
105+
new MethodErrorHandler(methodErrorHandling.statusCodesMap,
106+
classLevelStatusCodeDefinitions, methodDefault);
107+
108+
methodErrorHandlerMap.put(configKey(apiType, method), methodErrorHandler);
108109
}
110+
}
111+
112+
return methodErrorHandlerMap;
113+
}
109114

110-
Optional<ErrorHandling> readErrorHandlingIncludingInherited(Class<?> apiType) {
111-
if(apiType.isAnnotationPresent(ErrorHandling.class)) {
112-
return Optional.of(apiType.getAnnotation(ErrorHandling.class));
113-
}
114-
for(Class<?> parentInterface: apiType.getInterfaces()) {
115-
Optional<ErrorHandling> errorHandling = readErrorHandlingIncludingInherited(parentInterface);
116-
if(errorHandling.isPresent()) {
117-
return errorHandling;
118-
}
119-
}
120-
return Optional.empty();
115+
Optional<ErrorHandling> readErrorHandlingIncludingInherited(Class<?> apiType) {
116+
if (apiType.isAnnotationPresent(ErrorHandling.class)) {
117+
return Optional.of(apiType.getAnnotation(ErrorHandling.class));
118+
}
119+
for (Class<?> parentInterface : apiType.getInterfaces()) {
120+
Optional<ErrorHandling> errorHandling =
121+
readErrorHandlingIncludingInherited(parentInterface);
122+
if (errorHandling.isPresent()) {
123+
return errorHandling;
121124
}
125+
}
126+
return Optional.empty();
127+
}
122128

123-
static ErrorHandlingDefinition readAnnotation(ErrorHandling errorHandling, Decoder responseBodyDecoder) {
124-
ExceptionGenerator defaultException = new ExceptionGenerator.Builder()
125-
.withResponseBodyDecoder(responseBodyDecoder)
126-
.withExceptionType(errorHandling.defaultException())
127-
.build();
128-
Map<Integer, ExceptionGenerator> statusCodesDefinition = new HashMap<Integer, ExceptionGenerator>();
129-
130-
for(ErrorCodes statusCodeDefinition : errorHandling.codeSpecific()) {
131-
for(int statusCode : statusCodeDefinition.codes()) {
132-
if(statusCodesDefinition.containsKey(statusCode)) {
133-
throw new IllegalStateException(
134-
"Status Code [" + statusCode + "] " +
135-
"has already been declared to throw [" + statusCodesDefinition.get(statusCode).getExceptionType().getName() + "] " +
136-
"and [" + statusCodeDefinition.generate() + "] - dupe definition");
137-
}
138-
statusCodesDefinition.put(statusCode,
139-
new ExceptionGenerator.Builder()
140-
.withResponseBodyDecoder(responseBodyDecoder)
141-
.withExceptionType(statusCodeDefinition.generate())
142-
.build());
143-
}
144-
}
145-
146-
return new ErrorHandlingDefinition(defaultException, statusCodesDefinition);
129+
static ErrorHandlingDefinition readAnnotation(ErrorHandling errorHandling,
130+
Decoder responseBodyDecoder) {
131+
ExceptionGenerator defaultException = new ExceptionGenerator.Builder()
132+
.withResponseBodyDecoder(responseBodyDecoder)
133+
.withExceptionType(errorHandling.defaultException())
134+
.build();
135+
Map<Integer, ExceptionGenerator> statusCodesDefinition =
136+
new HashMap<Integer, ExceptionGenerator>();
137+
138+
for (ErrorCodes statusCodeDefinition : errorHandling.codeSpecific()) {
139+
for (int statusCode : statusCodeDefinition.codes()) {
140+
if (statusCodesDefinition.containsKey(statusCode)) {
141+
throw new IllegalStateException(
142+
"Status Code [" + statusCode + "] " +
143+
"has already been declared to throw ["
144+
+ statusCodesDefinition.get(statusCode).getExceptionType().getName() + "] " +
145+
"and [" + statusCodeDefinition.generate() + "] - dupe definition");
146+
}
147+
statusCodesDefinition.put(statusCode,
148+
new ExceptionGenerator.Builder()
149+
.withResponseBodyDecoder(responseBodyDecoder)
150+
.withExceptionType(statusCodeDefinition.generate())
151+
.build());
147152
}
153+
}
154+
155+
return new ErrorHandlingDefinition(defaultException, statusCodesDefinition);
156+
}
148157

149-
private static class ErrorHandlingDefinition {
150-
private final ExceptionGenerator defaultThrow;
151-
private final Map<Integer, ExceptionGenerator> statusCodesMap;
158+
private static class ErrorHandlingDefinition {
159+
private final ExceptionGenerator defaultThrow;
160+
private final Map<Integer, ExceptionGenerator> statusCodesMap;
152161

153162

154-
private ErrorHandlingDefinition(ExceptionGenerator defaultThrow, Map<Integer, ExceptionGenerator> statusCodesMap) {
155-
this.defaultThrow = defaultThrow;
156-
this.statusCodesMap = statusCodesMap;
157-
}
158-
}
163+
private ErrorHandlingDefinition(ExceptionGenerator defaultThrow,
164+
Map<Integer, ExceptionGenerator> statusCodesMap) {
165+
this.defaultThrow = defaultThrow;
166+
this.statusCodesMap = statusCodesMap;
167+
}
159168
}
169+
}
160170
}

0 commit comments

Comments
 (0)