1515 */
1616package feign .example .github ;
1717
18+ import java .io .IOException ;
1819import java .util .List ;
1920
2021import feign .Feign ;
2728import feign .gson .GsonDecoder ;
2829
2930/**
30- * adapted from {@code com.example.retrofit.GitHubClient}
31+ * Inspired by {@code com.example.retrofit.GitHubClient}
3132 */
3233public class GitHubExample {
3334
34- public static void main (String ... args ) throws InterruptedException {
35+ interface GitHub {
36+ @ RequestLine ("GET /repos/{owner}/{repo}/contributors" )
37+ List <Contributor > contributors (@ Param ("owner" ) String owner , @ Param ("repo" ) String repo );
38+ }
39+
40+ static class Contributor {
41+ String login ;
42+ int contributions ;
43+ }
44+
45+ static class GitHubClientError extends RuntimeException {
46+ private String message ; // parsed from json
47+
48+ @ Override
49+ public String getMessage () {
50+ return message ;
51+ }
52+ }
53+
54+ public static void main (String ... args ) {
3555 Decoder decoder = new GsonDecoder ();
3656 GitHub github = Feign .builder ()
3757 .decoder (decoder )
@@ -46,69 +66,30 @@ public static void main(String... args) throws InterruptedException {
4666 System .out .println (contributor .login + " (" + contributor .contributions + ")" );
4767 }
4868
69+ System .out .println ("Now, let's cause an error." );
4970 try {
50- contributors = github .contributors ("netflix" , "some-unknown-project" );
71+ github .contributors ("netflix" , "some-unknown-project" );
5172 } catch (GitHubClientError e ) {
52- System .out .println (e .error . message );
73+ System .out .println (e .getMessage () );
5374 }
5475 }
5576
56- interface GitHub {
57-
58- @ RequestLine ("GET /repos/{owner}/{repo}/contributors" )
59- List <Contributor > contributors (@ Param ("owner" ) String owner , @ Param ("repo" ) String repo );
60- }
61-
62- static class Contributor {
63-
64- String login ;
65- int contributions ;
66- }
67-
68- static class ClientError {
69-
70- String message ;
71- List <Error > errors ;
72- }
73-
74- static class Error {
75- String resource ;
76- String field ;
77- String code ;
78- }
79-
8077 static class GitHubErrorDecoder implements ErrorDecoder {
8178
8279 final Decoder decoder ;
8380 final ErrorDecoder defaultDecoder = new ErrorDecoder .Default ();
8481
85- public GitHubErrorDecoder (Decoder decoder ) {
82+ GitHubErrorDecoder (Decoder decoder ) {
8683 this .decoder = decoder ;
8784 }
8885
86+ @ Override
8987 public Exception decode (String methodKey , Response response ) {
90- if (response .status () >= 400 && response .status () < 500 ) {
91- try {
92- ClientError error = (ClientError ) decoder .decode (response , ClientError .class );
93- return new GitHubClientError (response .status (), error );
94- } catch (Exception e ) {
95- e .printStackTrace ();
96- }
88+ try {
89+ return (Exception ) decoder .decode (response , GitHubClientError .class );
90+ } catch (IOException fallbackToDefault ) {
91+ return defaultDecoder .decode (methodKey , response );
9792 }
98- return defaultDecoder .decode (methodKey , response );
99- }
100- }
101-
102- static class GitHubClientError extends RuntimeException {
103-
104- private static final long serialVersionUID = 0 ;
105-
106- ClientError error ;
107-
108- protected GitHubClientError (int status , ClientError error ) {
109- super ("client error " + status );
110- this .error = error ;
11193 }
112-
11394 }
11495}
0 commit comments