2323import feign .codec .Decoder ;
2424import feign .codec .ErrorDecoder ;
2525import feign .gson .GsonDecoder ;
26+ import java .io .IOException ;
2627import java .util .List ;
2728
28- /** adapted from {@code com.example.retrofit.GitHubClient} */
29+ /** Inspired by {@code com.example.retrofit.GitHubClient} */
2930public class GitHubExample {
3031
31- public static void main (String ... args ) throws InterruptedException {
32+ interface GitHub {
33+ @ RequestLine ("GET /repos/{owner}/{repo}/contributors" )
34+ List <Contributor > contributors (@ Param ("owner" ) String owner , @ Param ("repo" ) String repo );
35+ }
36+
37+ static class Contributor {
38+ String login ;
39+ int contributions ;
40+ }
41+
42+ static class GitHubClientError extends RuntimeException {
43+ private String message ; // parsed from json
44+
45+ @ Override
46+ public String getMessage () {
47+ return message ;
48+ }
49+ }
50+
51+ public static void main (String ... args ) {
3252 Decoder decoder = new GsonDecoder ();
3353 GitHub github =
3454 Feign .builder ()
@@ -44,68 +64,30 @@ public static void main(String... args) throws InterruptedException {
4464 System .out .println (contributor .login + " (" + contributor .contributions + ")" );
4565 }
4666
67+ System .out .println ("Now, let's cause an error." );
4768 try {
48- contributors = github .contributors ("netflix" , "some-unknown-project" );
69+ github .contributors ("netflix" , "some-unknown-project" );
4970 } catch (GitHubClientError e ) {
50- System .out .println (e .error . message );
71+ System .out .println (e .getMessage () );
5172 }
5273 }
5374
54- interface GitHub {
55-
56- @ RequestLine ("GET /repos/{owner}/{repo}/contributors" )
57- List <Contributor > contributors (@ Param ("owner" ) String owner , @ Param ("repo" ) String repo );
58- }
59-
60- static class Contributor {
61-
62- String login ;
63- int contributions ;
64- }
65-
66- static class ClientError {
67-
68- String message ;
69- List <Error > errors ;
70- }
71-
72- static class Error {
73- String resource ;
74- String field ;
75- String code ;
76- }
77-
7875 static class GitHubErrorDecoder implements ErrorDecoder {
7976
8077 final Decoder decoder ;
8178 final ErrorDecoder defaultDecoder = new ErrorDecoder .Default ();
8279
83- public GitHubErrorDecoder (Decoder decoder ) {
80+ GitHubErrorDecoder (Decoder decoder ) {
8481 this .decoder = decoder ;
8582 }
8683
84+ @ Override
8785 public Exception decode (String methodKey , Response response ) {
88- if (response .status () >= 400 && response .status () < 500 ) {
89- try {
90- ClientError error = (ClientError ) decoder .decode (response , ClientError .class );
91- return new GitHubClientError (response .status (), error );
92- } catch (Exception e ) {
93- e .printStackTrace ();
94- }
86+ try {
87+ return (Exception ) decoder .decode (response , GitHubClientError .class );
88+ } catch (IOException fallbackToDefault ) {
89+ return defaultDecoder .decode (methodKey , response );
9590 }
96- return defaultDecoder .decode (methodKey , response );
97- }
98- }
99-
100- static class GitHubClientError extends RuntimeException {
101-
102- private static final long serialVersionUID = 0 ;
103-
104- ClientError error ;
105-
106- protected GitHubClientError (int status , ClientError error ) {
107- super ("client error " + status );
108- this .error = error ;
10991 }
11092 }
11193}
0 commit comments