|
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 | | - |
24 | 22 | import static feign.Feign.configKey; |
25 | 23 |
|
26 | 24 | public class AnnotationErrorDecoder implements ErrorDecoder { |
27 | 25 |
|
28 | | - private final Map<String, MethodErrorHandler> errorHandlerMap; |
29 | | - private final ErrorDecoder defaultDecoder; |
| 26 | + private final Map<String, MethodErrorHandler> errorHandlerMap; |
| 27 | + private final ErrorDecoder defaultDecoder; |
30 | 28 |
|
31 | 29 |
|
32 | | - AnnotationErrorDecoder(Map<String, MethodErrorHandler> errorHandlerMap, ErrorDecoder defaultDecoder) { |
33 | | - this.errorHandlerMap = errorHandlerMap; |
34 | | - this.defaultDecoder = defaultDecoder; |
35 | | - } |
| 30 | + AnnotationErrorDecoder(Map<String, MethodErrorHandler> errorHandlerMap, |
| 31 | + ErrorDecoder defaultDecoder) { |
| 32 | + this.errorHandlerMap = errorHandlerMap; |
| 33 | + this.defaultDecoder = defaultDecoder; |
| 34 | + } |
36 | 35 |
|
37 | | - @Override |
38 | | - public Exception decode(String methodKey, Response response) { |
39 | | - if(errorHandlerMap.containsKey(methodKey)) { |
40 | | - return errorHandlerMap.get(methodKey).decode(response); |
41 | | - } |
42 | | - return defaultDecoder.decode(methodKey, response); |
| 36 | + @Override |
| 37 | + public Exception decode(String methodKey, Response response) { |
| 38 | + if (errorHandlerMap.containsKey(methodKey)) { |
| 39 | + return errorHandlerMap.get(methodKey).decode(response); |
43 | 40 | } |
| 41 | + return defaultDecoder.decode(methodKey, response); |
| 42 | + } |
44 | 43 |
|
45 | 44 |
|
46 | | - public static AnnotationErrorDecoder.Builder builderFor(Class<?> apiType) { |
47 | | - return new Builder(apiType); |
48 | | - } |
| 45 | + public static AnnotationErrorDecoder.Builder builderFor(Class<?> apiType) { |
| 46 | + return new Builder(apiType); |
| 47 | + } |
49 | 48 |
|
50 | | - public static class Builder { |
51 | | - private final Class<?> apiType; |
52 | | - private ErrorDecoder defaultDecoder = new ErrorDecoder.Default(); |
53 | | - private Decoder responseBodyDecoder = new Decoder.Default(); |
| 49 | + public static class Builder { |
| 50 | + private final Class<?> apiType; |
| 51 | + private ErrorDecoder defaultDecoder = new ErrorDecoder.Default(); |
| 52 | + private Decoder responseBodyDecoder = new Decoder.Default(); |
54 | 53 |
|
55 | 54 |
|
56 | | - public Builder(Class<?> apiType) { |
57 | | - this.apiType = apiType; |
58 | | - } |
| 55 | + public Builder(Class<?> apiType) { |
| 56 | + this.apiType = apiType; |
| 57 | + } |
59 | 58 |
|
60 | | - public Builder withDefaultDecoder(ErrorDecoder defaultDecoder) { |
61 | | - this.defaultDecoder = defaultDecoder; |
62 | | - return this; |
63 | | - } |
| 59 | + public Builder withDefaultDecoder(ErrorDecoder defaultDecoder) { |
| 60 | + this.defaultDecoder = defaultDecoder; |
| 61 | + return this; |
| 62 | + } |
64 | 63 |
|
65 | | - public Builder withResponseBodyDecoder(Decoder responseBodyDecoder) { |
66 | | - this.responseBodyDecoder = responseBodyDecoder; |
67 | | - return this; |
68 | | - } |
| 64 | + public Builder withResponseBodyDecoder(Decoder responseBodyDecoder) { |
| 65 | + this.responseBodyDecoder = responseBodyDecoder; |
| 66 | + return this; |
| 67 | + } |
69 | 68 |
|
70 | | - public AnnotationErrorDecoder build() { |
71 | | - Map<String, MethodErrorHandler> errorHandlerMap = generateErrorHandlerMapFromApi(apiType); |
72 | | - return new AnnotationErrorDecoder(errorHandlerMap, defaultDecoder); |
73 | | - } |
| 69 | + public AnnotationErrorDecoder build() { |
| 70 | + Map<String, MethodErrorHandler> errorHandlerMap = generateErrorHandlerMapFromApi(apiType); |
| 71 | + return new AnnotationErrorDecoder(errorHandlerMap, defaultDecoder); |
| 72 | + } |
74 | 73 |
|
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 = new HashMap<Integer, ExceptionGenerator>(); |
82 | | - |
83 | | - if(apiType.isAnnotationPresent(ErrorHandling.class)) { |
84 | | - ErrorHandlingDefinition classErrorHandlingDefinition = readAnnotation(apiType.getAnnotation(ErrorHandling.class), responseBodyDecoder); |
85 | | - classLevelDefault = classErrorHandlingDefinition.defaultThrow; |
86 | | - classLevelStatusCodeDefinitions = classErrorHandlingDefinition.statusCodesMap; |
87 | | - } |
88 | | - |
89 | | - Map<String, MethodErrorHandler> methodErrorHandlerMap = new HashMap<String, MethodErrorHandler>(); |
90 | | - for(Method method : apiType.getMethods()) { |
91 | | - if(method.isAnnotationPresent(ErrorHandling.class)) { |
92 | | - ErrorHandlingDefinition methodErrorHandling = readAnnotation(method.getAnnotation(ErrorHandling.class), responseBodyDecoder); |
93 | | - ExceptionGenerator methodDefault = methodErrorHandling.defaultThrow; |
94 | | - if(methodDefault.getExceptionType().equals(ErrorHandling.NO_DEFAULT.class)) { |
95 | | - methodDefault = classLevelDefault; |
96 | | - } |
97 | | - |
98 | | - MethodErrorHandler methodErrorHandler = |
99 | | - new MethodErrorHandler(methodErrorHandling.statusCodesMap, classLevelStatusCodeDefinitions, methodDefault ); |
100 | | - |
101 | | - methodErrorHandlerMap.put(configKey(apiType, method), methodErrorHandler); |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - return methodErrorHandlerMap; |
| 74 | + Map<String, MethodErrorHandler> generateErrorHandlerMapFromApi(Class<?> apiType) { |
| 75 | + |
| 76 | + ExceptionGenerator classLevelDefault = new ExceptionGenerator.Builder() |
| 77 | + .withResponseBodyDecoder(responseBodyDecoder) |
| 78 | + .withExceptionType(ErrorHandling.NO_DEFAULT.class) |
| 79 | + .build(); |
| 80 | + Map<Integer, ExceptionGenerator> classLevelStatusCodeDefinitions = |
| 81 | + new HashMap<Integer, ExceptionGenerator>(); |
| 82 | + |
| 83 | + if (apiType.isAnnotationPresent(ErrorHandling.class)) { |
| 84 | + ErrorHandlingDefinition classErrorHandlingDefinition = |
| 85 | + readAnnotation(apiType.getAnnotation(ErrorHandling.class), responseBodyDecoder); |
| 86 | + classLevelDefault = classErrorHandlingDefinition.defaultThrow; |
| 87 | + classLevelStatusCodeDefinitions = classErrorHandlingDefinition.statusCodesMap; |
| 88 | + } |
| 89 | + |
| 90 | + Map<String, MethodErrorHandler> methodErrorHandlerMap = |
| 91 | + new HashMap<String, MethodErrorHandler>(); |
| 92 | + for (Method method : apiType.getMethods()) { |
| 93 | + if (method.isAnnotationPresent(ErrorHandling.class)) { |
| 94 | + ErrorHandlingDefinition methodErrorHandling = |
| 95 | + readAnnotation(method.getAnnotation(ErrorHandling.class), responseBodyDecoder); |
| 96 | + ExceptionGenerator methodDefault = methodErrorHandling.defaultThrow; |
| 97 | + if (methodDefault.getExceptionType().equals(ErrorHandling.NO_DEFAULT.class)) { |
| 98 | + methodDefault = classLevelDefault; |
| 99 | + } |
| 100 | + |
| 101 | + MethodErrorHandler methodErrorHandler = |
| 102 | + new MethodErrorHandler(methodErrorHandling.statusCodesMap, |
| 103 | + classLevelStatusCodeDefinitions, methodDefault); |
| 104 | + |
| 105 | + methodErrorHandlerMap.put(configKey(apiType, method), methodErrorHandler); |
106 | 106 | } |
| 107 | + } |
107 | 108 |
|
108 | | - static ErrorHandlingDefinition readAnnotation(ErrorHandling errorHandling, Decoder responseBodyDecoder) { |
109 | | - ExceptionGenerator defaultException = new ExceptionGenerator.Builder() |
110 | | - .withResponseBodyDecoder(responseBodyDecoder) |
111 | | - .withExceptionType(errorHandling.defaultException()) |
112 | | - .build(); |
113 | | - Map<Integer, ExceptionGenerator> statusCodesDefinition = new HashMap<Integer, ExceptionGenerator>(); |
114 | | - |
115 | | - for(ErrorCodes statusCodeDefinition : errorHandling.codeSpecific()) { |
116 | | - for(int statusCode : statusCodeDefinition.codes()) { |
117 | | - if(statusCodesDefinition.containsKey(statusCode)) { |
118 | | - throw new IllegalStateException( |
119 | | - "Status Code [" + statusCode + "] " + |
120 | | - "has already been declared to throw [" + statusCodesDefinition.get(statusCode).getExceptionType().getName() + "] " + |
121 | | - "and [" + statusCodeDefinition.generate() + "] - dupe definition"); |
122 | | - } |
123 | | - statusCodesDefinition.put(statusCode, |
124 | | - new ExceptionGenerator.Builder() |
125 | | - .withResponseBodyDecoder(responseBodyDecoder) |
126 | | - .withExceptionType(statusCodeDefinition.generate()) |
127 | | - .build()); |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - return new ErrorHandlingDefinition(defaultException, statusCodesDefinition); |
| 109 | + return methodErrorHandlerMap; |
| 110 | + } |
| 111 | + |
| 112 | + static ErrorHandlingDefinition readAnnotation(ErrorHandling errorHandling, |
| 113 | + Decoder responseBodyDecoder) { |
| 114 | + ExceptionGenerator defaultException = new ExceptionGenerator.Builder() |
| 115 | + .withResponseBodyDecoder(responseBodyDecoder) |
| 116 | + .withExceptionType(errorHandling.defaultException()) |
| 117 | + .build(); |
| 118 | + Map<Integer, ExceptionGenerator> statusCodesDefinition = |
| 119 | + new HashMap<Integer, ExceptionGenerator>(); |
| 120 | + |
| 121 | + for (ErrorCodes statusCodeDefinition : errorHandling.codeSpecific()) { |
| 122 | + for (int statusCode : statusCodeDefinition.codes()) { |
| 123 | + if (statusCodesDefinition.containsKey(statusCode)) { |
| 124 | + throw new IllegalStateException( |
| 125 | + "Status Code [" + statusCode + "] " + |
| 126 | + "has already been declared to throw [" |
| 127 | + + statusCodesDefinition.get(statusCode).getExceptionType().getName() + "] " + |
| 128 | + "and [" + statusCodeDefinition.generate() + "] - dupe definition"); |
| 129 | + } |
| 130 | + statusCodesDefinition.put(statusCode, |
| 131 | + new ExceptionGenerator.Builder() |
| 132 | + .withResponseBodyDecoder(responseBodyDecoder) |
| 133 | + .withExceptionType(statusCodeDefinition.generate()) |
| 134 | + .build()); |
132 | 135 | } |
| 136 | + } |
| 137 | + |
| 138 | + return new ErrorHandlingDefinition(defaultException, statusCodesDefinition); |
| 139 | + } |
133 | 140 |
|
134 | | - private static class ErrorHandlingDefinition { |
135 | | - private final ExceptionGenerator defaultThrow; |
136 | | - private final Map<Integer, ExceptionGenerator> statusCodesMap; |
| 141 | + private static class ErrorHandlingDefinition { |
| 142 | + private final ExceptionGenerator defaultThrow; |
| 143 | + private final Map<Integer, ExceptionGenerator> statusCodesMap; |
137 | 144 |
|
138 | 145 |
|
139 | | - private ErrorHandlingDefinition(ExceptionGenerator defaultThrow, Map<Integer, ExceptionGenerator> statusCodesMap) { |
140 | | - this.defaultThrow = defaultThrow; |
141 | | - this.statusCodesMap = statusCodesMap; |
142 | | - } |
143 | | - } |
| 146 | + private ErrorHandlingDefinition(ExceptionGenerator defaultThrow, |
| 147 | + Map<Integer, ExceptionGenerator> statusCodesMap) { |
| 148 | + this.defaultThrow = defaultThrow; |
| 149 | + this.statusCodesMap = statusCodesMap; |
| 150 | + } |
144 | 151 | } |
| 152 | + } |
145 | 153 | } |
0 commit comments