-
Notifications
You must be signed in to change notification settings - Fork 11
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
Fix error thrown by connector when 429 response code is received #63
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSyntaxException; | ||
import io.cdap.plugin.servicenow.util.ServiceNowConstants; | ||
import org.apache.http.Header; | ||
import org.apache.http.HttpResponse; | ||
|
@@ -48,6 +49,7 @@ public class RestAPIResponse { | |
" },\n" + | ||
" \"status\": \"failure\"\n" + | ||
"}"; | ||
private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429; | ||
private int httpStatus; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All fields should be |
||
private Map<String, String> headers; | ||
private String responseBody; | ||
|
@@ -116,12 +118,31 @@ public boolean isSuccess() { | |
|
||
private void checkRetryable() { | ||
Gson gson = new Gson(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it a constant, no need to new it everytime. |
||
JsonObject jo = gson.fromJson(this.responseBody, JsonObject.class); | ||
if (jo.get(ServiceNowConstants.STATUS) != null && | ||
jo.get(ServiceNowConstants.STATUS).getAsString().equals(ServiceNowConstants.FAILURE) && | ||
jo.getAsJsonObject(ServiceNowConstants.ERROR).get(ServiceNowConstants.MESSAGE).getAsString() | ||
.contains(ServiceNowConstants.MAXIMUM_EXECUTION_TIME_EXCEEDED)) { | ||
isRetryable = true; | ||
try { | ||
JsonObject jo = gson.fromJson(this.responseBody, JsonObject.class); | ||
if (jo.get(ServiceNowConstants.STATUS) != null | ||
&& jo.get(ServiceNowConstants.STATUS).getAsString().equals(ServiceNowConstants.FAILURE) | ||
&& jo.getAsJsonObject(ServiceNowConstants.ERROR) | ||
.get(ServiceNowConstants.MESSAGE) | ||
.getAsString() | ||
.contains(ServiceNowConstants.MAXIMUM_EXECUTION_TIME_EXCEEDED)) { | ||
Comment on lines
+130
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you check if we require to handle null for the following in the responseBody json ?
|
||
isRetryable = true; | ||
} | ||
} catch (JsonSyntaxException e) { | ||
// Response Body is not a json object - check status code for error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we check the http status before parsing the body? Typically we should always check the status code before parsing the body. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately servicenow doesn't have any public documentation with an exhaustive list of response codes. For now going with what we know and will be adding them in the future. |
||
// Response body necessarily need not be json always (when 429 error is thrown - | ||
// responseBody is HTML) | ||
if (httpStatus == HTTP_STATUS_TOO_MANY_REQUESTS) { | ||
isRetryable = true; | ||
this.responseBody = | ||
String.format( | ||
JSON_ERROR_RESPONSE_TEMPLATE, | ||
"Too many requests to ServiceNow API - decrease concurrent requests"); | ||
} | ||
} catch (Throwable t) { | ||
// Any other exception | ||
isRetryable = false; | ||
this.responseBody = String.format(JSON_ERROR_RESPONSE_TEMPLATE, t.getMessage()); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong alignment