Skip to content

Commit

Permalink
Intercept 429 http error code and raise an explicit GenieClientTooMan…
Browse files Browse the repository at this point in the history
…yRequestException exception (#1169)

Co-authored-by: Andrew Nguyen <[email protected]>
  • Loading branch information
nvhoang and Andrew Nguyen authored Mar 28, 2022
1 parent c6df915 commit b7c596b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit b7c596b

Please sign in to comment.