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

Fix error thrown by connector when 429 response code is received #63

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

saimukkamala
Copy link
Contributor

  1. When ServiceNow API throws 429 error, response body is HTML (and not json as expected) which causes JsonSyntaxException while serializing to Json. This PR addresses this fix and throws a meaningful error.
  2. When ServiceNow API errors out when fetching table records, ServiceNowTableAPIClientImpl.fetchTableRecords returns empty list which can cause data loss. Now this will throw an error instead of empty list.

isRetryable = true;
}
} catch (JsonSyntaxException e) {
// Response Body is not a json object - check status code for error
Copy link

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@@ -26,5 +26,9 @@ public class RetryableException extends RuntimeException {
public RetryableException() {
super();
}

public RetryableException(String message) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong alignment

JSON_ERROR_RESPONSE_TEMPLATE,
"Too many requests to ServiceNow API - decrease concurrent requests");
}
Gson gson = new Gson();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it a constant, no need to new it everytime.

isRetryable = true;
this.responseBody =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we know the status is 429, why we need to create a json string, then parse it again just to get the status field as failure? Seem like we are overly complicating things here.

@@ -48,6 +49,7 @@ public class RestAPIResponse {
" },\n" +
" \"status\": \"failure\"\n" +
"}";
private static final int HTTP_STATUS_TOO_MANY_REQUESTS = 429;
private int httpStatus;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All fields should be final.

this.responseBody =
String.format(
JSON_ERROR_RESPONSE_TEMPLATE,
"Too many requests to ServiceNow API - decrease concurrent requests");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Too many requests to ServiceNow API - decrease concurrent requests");
"ServiceNow Concurrent requests quota reached, please wait for the running requests to complete before triggering any new concurrent request.");

Comment on lines +130 to +135
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ?

  1. Missing ERROR node
  2. Missing MESSAGE node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants