|
| 1 | +Annotation Error Decoder |
| 2 | +========================= |
| 3 | + |
| 4 | +This module allows to annotate Feign's interfaces with annotations to generate Exceptions based on error codes |
| 5 | + |
| 6 | +To use AnnotationErrorDecoder with Feign, add the Annotation Error Decoder module to your classpath. Then, configure |
| 7 | +Feign to use the AnnotationErrorDecoder: |
| 8 | + |
| 9 | +```java |
| 10 | +GitHub github = Feign.builder() |
| 11 | + .errorDecoder( |
| 12 | + AnnotationErrorDecoder.builderFor(GitHub.class).build() |
| 13 | + ) |
| 14 | + .target(GitHub.class, "https://api.github.com"); |
| 15 | +``` |
| 16 | + |
| 17 | +## Leveraging the annotations and priority order |
| 18 | +For annotation decoding to work, the class must be annotated with `@ErrorHandling` tags. |
| 19 | +The tags are valid in both the class level as well as method level. They will be treated from 'most specific' to |
| 20 | +'least specific' in the following order: |
| 21 | +* A code specific exception defined on the method |
| 22 | +* A code specific exception defined on the class |
| 23 | +* The default exception of the method |
| 24 | +* The default exception of the class |
| 25 | + |
| 26 | +```java |
| 27 | +@ErrorHandling(codeSpecific = |
| 28 | + { |
| 29 | + @ErrorCodes( codes = {401}, generate = UnAuthorizedException.class), |
| 30 | + @ErrorCodes( codes = {403}, generate = ForbiddenException.class), |
| 31 | + @ErrorCodes( codes = {404}, generate = UnknownItemException.class), |
| 32 | + }, |
| 33 | + defaultException = ClassLevelDefaultException.class |
| 34 | +) |
| 35 | +interface GitHub { |
| 36 | + |
| 37 | + @ErrorHandling(codeSpecific = |
| 38 | + { |
| 39 | + @ErrorCodes( codes = {404}, generate = NonExistentRepoException.class), |
| 40 | + @ErrorCodes( codes = {502, 503, 504}, generate = RetryAfterCertainTimeException.class), |
| 41 | + }, |
| 42 | + defaultException = FailedToGetContributorsException.class |
| 43 | + ) |
| 44 | + @RequestLine("GET /repos/{owner}/{repo}/contributors") |
| 45 | + List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); |
| 46 | +} |
| 47 | +``` |
| 48 | +In the above example, error responses to 'contributors' would hence be mapped as follows by status codes: |
| 49 | + |
| 50 | +| Code | Exception | Reason | |
| 51 | +| ----------- | -------------------------------- | --------------------- | |
| 52 | +| 401 | `UnAuthorizedException` | from Class definition | |
| 53 | +| 403 | `ForbiddenException` | from Class definition | |
| 54 | +| 404 | `NonExistenRepoException` | from Method definition, note that the class generic exception won't be thrown here | |
| 55 | +| 502,503,504 | `RetryAfterCertainTimeException` | from method definition. Note that you can have multiple error codes generate the same type of exception | |
| 56 | +| Any Other | `FailedToGetContributorsException` | from Method default | |
| 57 | + |
| 58 | +For a class level default exception to be thrown, the method must not have a `defaultException` defined, nor must the error code |
| 59 | +be mapped at either the method or class level. |
| 60 | + |
| 61 | +If the return code cannot be mapped to any code and no default exceptions have been configured, then the decoder will |
| 62 | +drop to a default decoder (by default, the standard one provided by feign). You can change the default drop-into decoder |
| 63 | +as follows: |
| 64 | + |
| 65 | +```java |
| 66 | +GitHub github = Feign.builder() |
| 67 | + .errorDecoder( |
| 68 | + AnnotationErrorDecoder.builderFor(GitHub.class) |
| 69 | + .withDefaultDecoder(new MyOtherErrorDecoder()) |
| 70 | + .build() |
| 71 | + ) |
| 72 | + .target(GitHub.class, "https://api.github.com"); |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +## Complex Exceptions |
| 77 | + |
| 78 | +Finally, Any exception can be used if they have a default constructor: |
| 79 | + |
| 80 | +```java |
| 81 | +class DefaultConstructorException extends Exception {} |
| 82 | +``` |
| 83 | + |
| 84 | +However, if you want to have parameters (such as the body in the error response or headers), you have to annotate its |
| 85 | +constructor appropriately (the body annotation is optional, provided there aren't paramters which will clash) |
| 86 | + |
| 87 | +All the following examples are valid exceptions: |
| 88 | +```java |
| 89 | +class JustBody extends Exception { |
| 90 | + |
| 91 | + @FeignExceptionConstructor |
| 92 | + public JustBody(String body) { |
| 93 | + |
| 94 | + } |
| 95 | +} |
| 96 | +//Headers must be of type Map<String, Collection<String>> |
| 97 | +class BodyAndHeaders extends Exception { |
| 98 | + |
| 99 | + @FeignExceptionConstructor |
| 100 | + public BodyAndHeaders(@ResponseBody String body, @ResponseHeaders Map<String, Collection<String>> headers) { |
| 101 | + |
| 102 | + } |
| 103 | +} |
| 104 | +class JustHeaders extends Exception { |
| 105 | + |
| 106 | + @FeignExceptionConstructor |
| 107 | + public JustHeaders(@ResponseHeaders Map<String, Collection<String>> headers) { |
| 108 | + |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +If you want to have the body decoded, you'll need to pass a decoder at construction time (just as for normal responses): |
| 114 | + |
| 115 | +```java |
| 116 | +GitHub github = Feign.builder() |
| 117 | + .errorDecoder( |
| 118 | + AnnotationErrorDecoder.builderFor(GitHub.class) |
| 119 | + .withResponseBodyDecoder(new JacksonDecoder()) |
| 120 | + .build() |
| 121 | + ) |
| 122 | + .target(GitHub.class, "https://api.github.com"); |
| 123 | +``` |
| 124 | + |
| 125 | +This will enable you to create exceptions where the body is a complex pojo: |
| 126 | + |
| 127 | +```java |
| 128 | +class ComplexPojoException extends Exception { |
| 129 | + |
| 130 | + @FeignExceptionConstructor |
| 131 | + public ComplexPojoException(GithubExceptionResponse body) { |
| 132 | + |
| 133 | + } |
| 134 | +} |
| 135 | +//The pojo can then be anything you'd like provided the decoder can manage it |
| 136 | +class GithubExceptionResponse { |
| 137 | + public String message; |
| 138 | + public int githubCode; |
| 139 | + public List<String> urlsForHelp; |
| 140 | +} |
| 141 | +``` |
0 commit comments