Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http status code to RetryableException and NonRetryableException #76

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> getHeaders() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down