diff --git a/genie-client/src/main/java/com/netflix/genie/client/exceptions/GenieClientTooManyRequestsException.java b/genie-client/src/main/java/com/netflix/genie/client/exceptions/GenieClientTooManyRequestsException.java new file mode 100644 index 00000000000..ec654a9985e --- /dev/null +++ b/genie-client/src/main/java/com/netflix/genie/client/exceptions/GenieClientTooManyRequestsException.java @@ -0,0 +1,44 @@ +/* + * + * Copyright 2022 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.netflix.genie.client.exceptions; + +/** + * An exception class that represents 429 - Too Many Requests received from the server. + * + * @author andrew + * @since 4.0.0 + */ +public class GenieClientTooManyRequestsException extends GenieClientException { + + /** + * Constructor. + * + */ + public GenieClientTooManyRequestsException() { + super(429, ""); + } + + /** + * Constructor. + * + * @param msg human readable message + */ + public GenieClientTooManyRequestsException(final String msg) { + super(429, msg); + } +} diff --git a/genie-client/src/main/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptor.java b/genie-client/src/main/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptor.java index 38784aa9215..798944947ab 100644 --- a/genie-client/src/main/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptor.java +++ b/genie-client/src/main/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptor.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.netflix.genie.client.exceptions.GenieClientException; +import com.netflix.genie.client.exceptions.GenieClientTooManyRequestsException; import com.netflix.genie.common.external.util.GenieObjectMapper; import okhttp3.Interceptor; import okhttp3.Response; @@ -38,6 +39,7 @@ public class ResponseMappingInterceptor implements Interceptor { private static final String NO_MESSAGE_FALLBACK = "No error detailed message in server response"; private static final String ERROR_MESSAGE_KEY = "message"; + private static final int HTTP_TOO_MANY_REQUESTS = 429; /** * Constructor. @@ -66,8 +68,12 @@ public Response intercept(final Chain chain) throws IOException { responseBody == null || !responseBody.has(ERROR_MESSAGE_KEY) ? NO_MESSAGE_FALLBACK : responseBody.get(ERROR_MESSAGE_KEY).asText(); + final int responseCode = response.code(); + if (responseCode == HTTP_TOO_MANY_REQUESTS) { + throw new GenieClientTooManyRequestsException(errorMessage); + } throw new GenieClientException( - response.code(), + responseCode, errorMessage ); } catch (final JsonProcessingException jpe) { diff --git a/genie-client/src/test/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptorTest.java b/genie-client/src/test/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptorTest.java index 941b3b2ec09..5a1a640da64 100644 --- a/genie-client/src/test/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptorTest.java +++ b/genie-client/src/test/java/com/netflix/genie/client/interceptors/ResponseMappingInterceptorTest.java @@ -18,6 +18,7 @@ package com.netflix.genie.client.interceptors; import com.netflix.genie.client.exceptions.GenieClientException; +import com.netflix.genie.client.exceptions.GenieClientTooManyRequestsException; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -103,4 +104,13 @@ void canInterceptFailure() { .withMessage("Failed to parse server response as JSON"); } + + @Test + void canIntercept429() { + this.server.enqueue(new MockResponse().setResponseCode(429)); + final Request request = new Request.Builder().url(this.baseUrl).get().build(); + Assertions + .assertThatExceptionOfType(GenieClientTooManyRequestsException.class) + .isThrownBy(() -> this.client.newCall(request).execute()); + } }