Skip to content

Commit

Permalink
Merge pull request #1228 from krgauraw/3.3.0_error_handling_fixes
Browse files Browse the repository at this point in the history
Issue #SB-20671 fix: code changes for error handling
  • Loading branch information
amitpriyadarshi authored Oct 15, 2020
2 parents 7aec8eb + 26c1fb4 commit 8511013
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
2 changes: 1 addition & 1 deletion platform-jobs/samza/auto-creator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>auto-creator</artifactId>
<version>0.0.23</version>
<version>0.0.25</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ private void updateStatus(String channelId, String identifier, String message) t
else
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Content update status Call Failed For : " + identifier);
} else {
LOGGER.info("ContentUtil :: updateStatus :: Invalid Response received while updating failed status for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while updating content status for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: updateStatus :: Invalid Response received while updating failed status for : " + identifier + getErrorDetails(resp));
throw new ServerException("ERR_API_CALL", "Invalid Response received while updating content status for : " + identifier + getErrorDetails(resp));
}
}

Expand Down Expand Up @@ -225,8 +225,8 @@ private Map<String, Object> searchContent(String identifier) throws Exception {
LOGGER.info("ContentUtil :: searchContent :: Received 0 count while searching content for : " + identifier);

} else {
LOGGER.info("ContentUtil :: searchContent :: Invalid Response received while searching content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString());
throw new ServerException("ERR_API_CALL", "Invalid Response received while searching content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: searchContent :: Invalid Response received while searching content for : " + identifier + getErrorDetails(resp));
throw new ServerException("ERR_API_CALL", "Invalid Response received while searching content for : " + identifier + getErrorDetails(resp));
}
return result;
}
Expand Down Expand Up @@ -275,8 +275,8 @@ private Map<String, Object> create(String channelId, String identifier, String n
contentId = (String) resp.getResult().get("identifier");
LOGGER.info("ContentUtil :: create :: Content Created Successfully with identifier : " + contentId);
} else {
LOGGER.info("ContentUtil :: create :: Invalid Response received while creating content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while creating content for : " + identifier+ " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: create :: Invalid Response received while creating content for : " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while creating content for : " + identifier+ getErrorDetails(resp));
}
return resp.getResult();
}
Expand All @@ -296,8 +296,8 @@ private Map<String, Object> read(String channelId, String identifier) throws Exc
LOGGER.info("ContentUtil :: read :: Content Fetched Successfully with identifier : " + contentId);
else throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while reading content for : " + identifier);
} else {
LOGGER.info("ContentUtil :: read :: Invalid Response received while reading content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while reading content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: read :: Invalid Response received while reading content for : " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while reading content for : " + identifier + getErrorDetails(resp));
}
return ((Map<String, Object>) resp.getResult().getOrDefault("content", new HashMap<String, Object>()));
}
Expand All @@ -319,8 +319,8 @@ private void update(String channelId, String internalId, Map<String, Object> upd
String contentId = (String) resp.getResult().get("identifier");
LOGGER.info("ContentUtil :: update :: Content Update Successfully having identifier : " + contentId);
} else {
LOGGER.info("ContentUtil :: update :: Invalid Response received while updating content for : " + internalId + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while updating content for : " + internalId + "| Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: update :: Invalid Response received while updating content for : " + internalId + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while updating content for : " + internalId + getErrorDetails(resp));
}
}

Expand All @@ -341,7 +341,7 @@ private void update(String channelId, String internalId, Map<String, Object> upd
if (StringUtils.isNotBlank(artifactUrl) && StringUtils.equalsIgnoreCase(fileUrl, artifactUrl))
LOGGER.info("ContentUtil :: upload :: Content Uploaded Successfully for : " + identifier + " | artifactUrl : " + artifactUrl);
} else {
LOGGER.info("ContentUtil :: upload :: Invalid Response received while uploading for: " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: upload :: Invalid Response received while uploading for: " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while uploading : " + identifier);
}
} else {
Expand Down Expand Up @@ -400,8 +400,8 @@ private Boolean upload(String channelId, String identifier, Map<String, Object>
return true;
}
} else {
LOGGER.info("ContentUtil :: upload :: Invalid Response received while uploading for: " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while uploading : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: upload :: Invalid Response received while uploading for: " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while uploading : " + identifier + getErrorDetails(resp));
}
return false;
}
Expand All @@ -426,8 +426,8 @@ private Boolean review(String channelId, String identifier) throws Exception {
return true;
}
} else {
LOGGER.info("ContentUtil :: review :: Invalid Response received while sending content to review for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while sending content to review for : " + identifier + "| Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: review :: Invalid Response received while sending content to review for : " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while sending content to review for : " + identifier + getErrorDetails(resp));
}
return false;
}
Expand Down Expand Up @@ -455,8 +455,8 @@ private Boolean publish(String channelId, String identifier, String lastPublishe
else
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Content Publish Call Failed For : " + identifier);
} else {
LOGGER.info("ContentUtil :: publish :: Invalid Response received while publishing content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while publishing content for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: publish :: Invalid Response received while publishing content for : " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while publishing content for : " + identifier + getErrorDetails(resp));
}

}
Expand All @@ -479,8 +479,8 @@ private String getPreSignedUrl(String identifier, String fileName) throws Except
preSignedUrl = (String) resp.getResult().get("pre_signed_url");
return preSignedUrl;
} else {
LOGGER.info("ContentUtil :: getPreSignedUrl :: Invalid Response received while generating pre-signed url for : " + identifier + " | Response Code : " + resp.getResponseCode().toString());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while generating pre-signed url for : " + identifier);
LOGGER.info("ContentUtil :: getPreSignedUrl :: Invalid Response received while generating pre-signed url for : " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while generating pre-signed url for : " + identifier + getErrorDetails(resp));
}
}

Expand Down Expand Up @@ -576,8 +576,8 @@ private Boolean addToHierarchy(String channel, String textbookId, Map<String, Ob
result = true;
}
} else {
LOGGER.info("ContentUtil :: updateHierarchy :: Invalid Response received while adding resource to hierarchy for : " + textbookId + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while adding resource to hierarchy for : " + textbookId + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: updateHierarchy :: Invalid Response received while adding resource to hierarchy for : " + textbookId + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while adding resource to hierarchy for : " + textbookId + getErrorDetails(resp));
}
return result;
}
Expand All @@ -593,8 +593,8 @@ private Map<String, Object> getHierarchy(String identifier) throws Exception {
result = (Map<String, Object>) resp.getResult().getOrDefault("content", new HashMap<String, Object>());
return result;
} else {
LOGGER.info("ContentUtil :: getHierarchy :: Invalid Response received while fetching hierarchy for : " + identifier + " | Response Code : " + resp.getResponseCode().toString());
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while fetching hierarchy for : " + identifier + " | Response Code : " + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg());
LOGGER.info("ContentUtil :: getHierarchy :: Invalid Response received while fetching hierarchy for : " + identifier + getErrorDetails(resp));
throw new ServerException(TaxonomyErrorCodes.SYSTEM_ERROR.name(), "Invalid Response received while fetching hierarchy for : " + identifier + getErrorDetails(resp));
}
}

Expand Down Expand Up @@ -656,4 +656,8 @@ private void delay(long time) {
}
}

private static String getErrorDetails(Response resp) {
return (null != resp) ? (" | Response Code :" + resp.getResponseCode().toString() + " | Result : " + resp.getResult() + " | Error Message : " + resp.getParams().getErrmsg()) : " | Null Response Received.";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public static Response post(String url, Map<String, Object> requestMap, Map<Stri
if (MapUtils.isEmpty(requestMap))
throw new ServerException("ERR_INVALID_REQUEST_BODY", "Request Body is Missing!");
try {
while (null == resp && BACKOFF_DELAY <= MAXIMUM_BACKOFF_DELAY) {
while (null == resp) {
HttpResponse<String> response = Unirest.post(url).headers(headerParam).body(mapper.writeValueAsString(requestMap)).asString();
resp = getResponse(response);
resp = getResponse(url, response);
}
return resp;
} catch (Exception e) {
Expand All @@ -46,9 +46,9 @@ public static Response patch(String url, Map<String, Object> requestMap, Map<Str
if (MapUtils.isEmpty(requestMap))
throw new ServerException("ERR_INVALID_REQUEST_BODY", "Request Body is Missing!");
try {
while (null == resp && BACKOFF_DELAY <= MAXIMUM_BACKOFF_DELAY) {
while (null == resp) {
HttpResponse<String> response = Unirest.patch(url).headers(headerParam).body(mapper.writeValueAsString(requestMap)).asString();
resp = getResponse(response);
resp = getResponse(url, response);
}
return resp;
} catch (Exception e) {
Expand All @@ -63,9 +63,9 @@ public static Response post(String url, String paramName, File value, Map<String
if (null == value || null == value)
throw new ServerException("ERR_INVALID_REQUEST_PARAM", "Invalid Request Param!");
try {
while (null == resp && BACKOFF_DELAY <= MAXIMUM_BACKOFF_DELAY) {
while (null == resp) {
HttpResponse<String> response = Unirest.post(url).headers(headerParam).multiPartContent().field(paramName, new File(value.getAbsolutePath())).asString();
resp = getResponse(response);
resp = getResponse(url, response);
}
return resp;
} catch (Exception e) {
Expand All @@ -80,9 +80,9 @@ public static Response post(String url, String paramName, String value, Map<Stri
if (null == value || null == value)
throw new ServerException("ERR_INVALID_REQUEST_PARAM", "Invalid Request Param!");
try {
while (null == resp && BACKOFF_DELAY <= MAXIMUM_BACKOFF_DELAY) {
while (null == resp) {
HttpResponse<String> response = Unirest.post(url).headers(headerParam).multiPartContent().field(paramName, value).asString();
resp = getResponse(response);
resp = getResponse(url, response);
}
return resp;
} catch (Exception e) {
Expand All @@ -96,9 +96,9 @@ public static Response get(String url, String queryParam, Map<String, String> he
validateRequest(url, headerParam);
String reqUrl = StringUtils.isNotBlank(queryParam) ? url + "?" + queryParam : url;
try {
while (null == resp && BACKOFF_DELAY <= MAXIMUM_BACKOFF_DELAY) {
while (null == resp) {
HttpResponse<String> response = Unirest.get(reqUrl).headers(headerParam).asString();
resp = getResponse(response);
resp = getResponse(reqUrl, response);
}
return resp;
} catch (Exception e) {
Expand All @@ -113,19 +113,21 @@ private static void validateRequest(String url, Map<String, String> headerParam)
throw new ServerException("ERR_INVALID_HEADER_PARAM", "Header Parameter is Missing!");
}

private static Response getResponse(HttpResponse<String> response) {
private static Response getResponse(String url, HttpResponse<String> response) {
Response resp = null;
if (null != response && StringUtils.isNotBlank(response.getBody())) {
try {
resp = mapper.readValue(response.getBody(), Response.class);
BACKOFF_DELAY = INITIAL_BACKOFF_DELAY;
} catch (Exception e) {
LOGGER.error("UnirestUtil ::: getResponse ::: Error occurred while parsing api response. Error is: "+e.getMessage(), e);
LOGGER.error("UnirestUtil ::: getResponse ::: Error occurred while parsing api response for url ::: " + url + ". | Error is: " + e.getMessage(), e);
LOGGER.info("UnirestUtil :::: BACKOFF_DELAY ::: " + BACKOFF_DELAY);
if (BACKOFF_DELAY <= MAXIMUM_BACKOFF_DELAY) {
long delay = BACKOFF_DELAY;
BACKOFF_DELAY = BACKOFF_DELAY * INCREMENT_BACKOFF_DELAY;
LOGGER.info("UnirestUtil :::: BACKOFF_DELAY after increment::: " + BACKOFF_DELAY);
delay(delay);
} else throw new ServerException("ERR_API_CALL", "Unable to parse response data! | Error is: " + e.getMessage());
} else throw new ServerException("ERR_API_CALL", "Unable to parse response data for url: "+ url +" | Error is: " + e.getMessage());
}
} else {
LOGGER.info("Null Response Received While Making Api Call!");
Expand Down
2 changes: 1 addition & 1 deletion platform-jobs/samza/distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<dependency>
<groupId>org.ekstep</groupId>
<artifactId>auto-creator</artifactId>
<version>0.0.23</version>
<version>0.0.25</version>
<type>tar.gz</type>
<classifier>distribution</classifier>
</dependency>
Expand Down

0 comments on commit 8511013

Please sign in to comment.