From 08c3261611a485371eb6fa925b3a329ced37183e Mon Sep 17 00:00:00 2001 From: Sai Aditya Mukkamala Date: Mon, 20 May 2024 13:20:14 +0530 Subject: [PATCH] Add http status code to RetryableException and NonRetryableException --- .../apiclient/ExceptionWithHttpStatus.java | 10 ++++++++++ .../apiclient/NonRetryableException.java | 19 ++++++++++++++++++- .../apiclient/RetryableException.java | 19 ++++++++++++++++++- .../servicenow/restapi/RestAPIResponse.java | 4 ++-- .../servicenow/restapi/RestAPIClientTest.java | 17 ++++++++++++++--- 5 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/cdap/plugin/servicenow/apiclient/ExceptionWithHttpStatus.java diff --git a/src/main/java/io/cdap/plugin/servicenow/apiclient/ExceptionWithHttpStatus.java b/src/main/java/io/cdap/plugin/servicenow/apiclient/ExceptionWithHttpStatus.java new file mode 100644 index 0000000..2661a6e --- /dev/null +++ b/src/main/java/io/cdap/plugin/servicenow/apiclient/ExceptionWithHttpStatus.java @@ -0,0 +1,10 @@ +package io.cdap.plugin.servicenow.apiclient; + +import javax.annotation.Nullable; + +/** + * Exception which contains a Http Status Code. + */ +public interface ExceptionWithHttpStatus { + @Nullable Integer getHttpStatusCode(); +} diff --git a/src/main/java/io/cdap/plugin/servicenow/apiclient/NonRetryableException.java b/src/main/java/io/cdap/plugin/servicenow/apiclient/NonRetryableException.java index 7bf2342..858db4b 100644 --- a/src/main/java/io/cdap/plugin/servicenow/apiclient/NonRetryableException.java +++ b/src/main/java/io/cdap/plugin/servicenow/apiclient/NonRetryableException.java @@ -16,20 +16,37 @@ package io.cdap.plugin.servicenow.apiclient; +import org.jetbrains.annotations.Nullable; + /** Custom Exception Class for handling retrying API calls */ -public class NonRetryableException extends RuntimeException { +public class NonRetryableException extends RuntimeException implements ExceptionWithHttpStatus { private static final long serialVersionUID = 1L; + private final Integer httpStatusCode; public NonRetryableException() { super(); + this.httpStatusCode = null; } public NonRetryableException(String message) { super(message); + this.httpStatusCode = null; } public NonRetryableException(String message, Throwable throwable) { super(message, throwable); + this.httpStatusCode = null; + } + + public NonRetryableException(String message, Integer httpStatusCode) { + super(message); + this.httpStatusCode = httpStatusCode; + } + + @Nullable + @Override + public Integer getHttpStatusCode() { + return httpStatusCode; } } diff --git a/src/main/java/io/cdap/plugin/servicenow/apiclient/RetryableException.java b/src/main/java/io/cdap/plugin/servicenow/apiclient/RetryableException.java index bf1cd71..bdd0b59 100644 --- a/src/main/java/io/cdap/plugin/servicenow/apiclient/RetryableException.java +++ b/src/main/java/io/cdap/plugin/servicenow/apiclient/RetryableException.java @@ -16,20 +16,37 @@ package io.cdap.plugin.servicenow.apiclient; +import org.jetbrains.annotations.Nullable; + /** Custom Exception Class for handling retrying API calls */ -public class RetryableException extends RuntimeException { +public class RetryableException extends RuntimeException implements ExceptionWithHttpStatus { private static final long serialVersionUID = 1L; + private final Integer httpStatusCode; public RetryableException() { super(); + this.httpStatusCode = null; + } + + public RetryableException(String message, Integer httpStatusCode) { + super(message); + this.httpStatusCode = httpStatusCode; } public RetryableException(String message) { super(message); + this.httpStatusCode = null; } public RetryableException(String message, Throwable throwable) { super(message, throwable); + this.httpStatusCode = null; + } + + @Nullable + @Override + public Integer getHttpStatusCode() { + return httpStatusCode; } } diff --git a/src/main/java/io/cdap/plugin/servicenow/restapi/RestAPIResponse.java b/src/main/java/io/cdap/plugin/servicenow/restapi/RestAPIResponse.java index be0a0b1..2953174 100644 --- a/src/main/java/io/cdap/plugin/servicenow/restapi/RestAPIResponse.java +++ b/src/main/java/io/cdap/plugin/servicenow/restapi/RestAPIResponse.java @@ -118,9 +118,9 @@ private static void validateHttpResponse(HttpResponse response) { return; } if (RETRYABLE_CODES.contains(code)) { - throw new RetryableException(String.format(HTTP_ERROR_MESSAGE, code)); + throw new RetryableException(String.format(HTTP_ERROR_MESSAGE, code), code); } - throw new NonRetryableException(String.format(HTTP_ERROR_MESSAGE, code)); + throw new NonRetryableException(String.format(HTTP_ERROR_MESSAGE, code), code); } public Map getHeaders() { diff --git a/src/test/java/io/cdap/plugin/servicenow/restapi/RestAPIClientTest.java b/src/test/java/io/cdap/plugin/servicenow/restapi/RestAPIClientTest.java index 9c72328..5a68355 100644 --- a/src/test/java/io/cdap/plugin/servicenow/restapi/RestAPIClientTest.java +++ b/src/test/java/io/cdap/plugin/servicenow/restapi/RestAPIClientTest.java @@ -12,13 +12,13 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicStatusLine; import org.apache.http.util.EntityUtils; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; - import java.io.IOException; @RunWith(PowerMockRunner.class) @@ -50,7 +50,12 @@ public void testExecuteGet_throwRetryableException() throws IOException { ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class); ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config); - client.executeGet(request); + try { + client.executeGet(request); + } catch (RetryableException e) { + Assert.assertEquals(Integer.valueOf(429), e.getHttpStatusCode()); + throw e; + } } @Test(expected = NonRetryableException.class) @@ -72,7 +77,13 @@ public void testExecuteGet_throwIOException() throws IOException { ServiceNowConnectorConfig config = Mockito.mock(ServiceNowConnectorConfig.class); ServiceNowTableAPIClientImpl client = new ServiceNowTableAPIClientImpl(config); - client.executeGet(request); + try { + client.executeGet(request); + } catch (NonRetryableException e) { + Assert.assertEquals( + Integer.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR), e.getHttpStatusCode()); + throw e; + } } @Test