-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error propagation for API calls (#78)
* error propagation for API calls * refactor ServiceNowAPIException constructors Co-authored-by: Rahul Sharma <[email protected]> --------- Co-authored-by: Rahul Sharma <[email protected]> semicolon bug fix
- Loading branch information
1 parent
ab865c5
commit a113703
Showing
25 changed files
with
434 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/main/java/io/cdap/plugin/servicenow/apiclient/NonRetryableException.java
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
src/main/java/io/cdap/plugin/servicenow/apiclient/RetryableException.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
src/main/java/io/cdap/plugin/servicenow/apiclient/ServiceNowAPIException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.cdap.plugin.servicenow.apiclient; | ||
|
||
import io.cdap.plugin.servicenow.util.ServiceNowConstants; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.oltu.oauth2.common.exception.OAuthSystemException; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Custom Exception class for propagating API errors/exceptions back to caller. | ||
*/ | ||
public class ServiceNowAPIException extends Exception { | ||
|
||
@Nullable private final HttpResponse httpResponse; | ||
@Nullable private final boolean manualRetry; | ||
|
||
private static final Set<Integer> RETRYABLE_CODES = new HashSet<>(Arrays.asList(429, | ||
HttpStatus.SC_BAD_GATEWAY, | ||
HttpStatus.SC_SERVICE_UNAVAILABLE, | ||
HttpStatus.SC_REQUEST_TIMEOUT, | ||
HttpStatus.SC_GATEWAY_TIMEOUT)); | ||
|
||
public ServiceNowAPIException(String message, @Nullable HttpResponse httpResponse) { | ||
this(message, null, httpResponse, false); | ||
} | ||
|
||
public ServiceNowAPIException(Throwable t, @Nullable HttpResponse httpResponse) { | ||
this(null, t, httpResponse, false); | ||
} | ||
|
||
public ServiceNowAPIException(String message, Throwable t, | ||
@Nullable HttpResponse httpResponse, boolean manualRetry) { | ||
super(message, t); | ||
this.httpResponse = httpResponse; | ||
this.manualRetry = manualRetry; | ||
} | ||
|
||
public String getUnderlyingMessage() { | ||
if (this.getCause() != null) { | ||
return this.getCause().getMessage(); | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
public HttpResponse getHttpResponse() { | ||
return httpResponse; | ||
} | ||
|
||
public int getStatusCode() { | ||
if (httpResponse != null && httpResponse.getStatusLine() != null) { | ||
return httpResponse.getStatusLine().getStatusCode(); | ||
} | ||
return 0; | ||
} | ||
|
||
public boolean isErrorRetryable() { | ||
if (manualRetry) { | ||
return true; | ||
} | ||
Throwable t = this.getCause(); | ||
return t instanceof OAuthSystemException | ||
|| (this.getMessage() != null | ||
&& this.getMessage().contains(ServiceNowConstants.MAXIMUM_EXECUTION_TIME_EXCEEDED)) | ||
|| RETRYABLE_CODES.contains(getStatusCode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.