|
16 | 16 | import feign.Response; |
17 | 17 | import feign.codec.Decoder; |
18 | 18 | import feign.codec.ErrorDecoder; |
19 | | - |
20 | 19 | import java.lang.reflect.Method; |
21 | 20 | import java.util.HashMap; |
22 | 21 | import java.util.Map; |
23 | 22 | import java.util.Optional; |
24 | | - |
25 | 23 | import static feign.Feign.configKey; |
26 | 24 |
|
27 | 25 | public class AnnotationErrorDecoder implements ErrorDecoder { |
28 | 26 |
|
29 | | - private final Map<String, MethodErrorHandler> errorHandlerMap; |
30 | | - private final ErrorDecoder defaultDecoder; |
| 27 | + private final Map<String, MethodErrorHandler> errorHandlerMap; |
| 28 | + private final ErrorDecoder defaultDecoder; |
31 | 29 |
|
32 | 30 |
|
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 | + } |
37 | 36 |
|
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); |
44 | 41 | } |
| 42 | + return defaultDecoder.decode(methodKey, response); |
| 43 | + } |
45 | 44 |
|
46 | 45 |
|
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 | + } |
50 | 49 |
|
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(); |
55 | 54 |
|
56 | 55 |
|
57 | | - public Builder(Class<?> apiType) { |
58 | | - this.apiType = apiType; |
59 | | - } |
| 56 | + public Builder(Class<?> apiType) { |
| 57 | + this.apiType = apiType; |
| 58 | + } |
60 | 59 |
|
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 | + } |
65 | 64 |
|
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 | + } |
70 | 69 |
|
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 | + } |
75 | 74 |
|
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); |
108 | 109 | } |
| 110 | + } |
| 111 | + |
| 112 | + return methodErrorHandlerMap; |
| 113 | + } |
109 | 114 |
|
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; |
121 | 124 | } |
| 125 | + } |
| 126 | + return Optional.empty(); |
| 127 | + } |
122 | 128 |
|
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()); |
147 | 152 | } |
| 153 | + } |
| 154 | + |
| 155 | + return new ErrorHandlingDefinition(defaultException, statusCodesDefinition); |
| 156 | + } |
148 | 157 |
|
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; |
152 | 161 |
|
153 | 162 |
|
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 | + } |
159 | 168 | } |
| 169 | + } |
160 | 170 | } |
0 commit comments