44import com .google .common .io .CharStreams ;
55import feign .Response ;
66import feign .codec .ErrorDecoder ;
7+ import io .ipdata .client .error .RateLimitException ;
78import io .ipdata .client .error .RemoteIpdataException ;
89import io .ipdata .client .model .Error ;
910import java .io .IOException ;
1314
1415@ RequiredArgsConstructor
1516public class ApiErrorDecoder implements ErrorDecoder {
16-
17+ private static final int RATE_LIMIT_STATUS = 403 ;
1718 private final ObjectMapper mapper ;
1819 private final Logger logger ;
1920
@@ -22,12 +23,17 @@ public Exception decode(String methodKey, Response response) {
2223 String message = null ;
2324 try {
2425 //parse the url, but would strip the query parameters containing sensitive information (api-key)
26+ int responseCode = response .status ();
2527 URL url = new URL (response .request ().url ());
2628 logger .error ("An error occurred during an Ipdata API call: got status '{}' for path '{}'" ,
27- response . status () , url .getPath ());
29+ responseCode , url .getPath ());
2830 message = CharStreams .toString (response .body ().asReader ());
2931 Error error = mapper .readValue (message , Error .class );
30- return new RemoteIpdataException (error .getMessage (), response .status ());
32+ if (responseCode == RATE_LIMIT_STATUS ) {
33+ return new RateLimitException (error .getMessage (), responseCode );
34+ } else {
35+ return new RemoteIpdataException (error .getMessage (), response .status ());
36+ }
3137 } catch (IOException ioException ) {
3238 ioException .printStackTrace ();
3339 return new RemoteIpdataException (message , response .status ());
0 commit comments