Skip to content

Commit

Permalink
Improved exception handling of ObjectMapper exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: coduz <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Sep 29, 2020
1 parent 5c99ada commit 39435a7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,38 @@
*/
public class ClientInternalError extends ClientException {

private final String reason;

/**
* Constructor.
*
* @param reason The reason that caused the {@link ClientInternalError}.
* @since 1.3.0
*/
public ClientInternalError() {
super(ClientErrorCodes.INTERNAL_ERROR);
public ClientInternalError(String reason) {
this(null, reason);
}


/**
* Constructor.
*
* @param cause The root {@link Throwable} of this {@link ClientInternalError}.
* @param cause The root {@link Throwable} of this {@link ClientInternalError}.
* @param reason The reason that caused the {@link ClientInternalError}.
* @since 1.3.0
*/
public ClientInternalError(Throwable cause, String reason) {
super(ClientErrorCodes.INTERNAL_ERROR, cause, reason);

this.reason = reason;
}

/**
* The reason that caused this {@link ClientInternalError}.
*
* @return The reason that caused this {@link ClientInternalError}.
* @since 1.3.0
*/
public ClientInternalError(Throwable cause) {
super(ClientErrorCodes.INTERNAL_ERROR, cause);
public String getReason() {
return reason;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,12 @@ public void close() {
public InsertResponse insert(InsertRequest insertRequest) throws ClientException {
Map<String, Object> storableMap = getModelContext().marshal(insertRequest.getStorable());
LOG.debug("Insert - converted object: '{}'", storableMap);

String json;
try {
json = objectMapper.writeValueAsString(storableMap);
} catch (JsonProcessingException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.write");
}

Response insertResponse = restCallTimeoutHandler(() -> getClient()
Expand All @@ -150,7 +151,7 @@ public InsertResponse insert(InsertRequest insertRequest) throws ClientException
try {
responseNode = objectMapper.readTree(EntityUtils.toString(insertResponse.getEntity()));
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.read");
}

String id = responseNode.get(ElasticsearchKeywords.KEY_DOC_ID).asText();
Expand All @@ -174,7 +175,7 @@ public UpdateResponse upsert(UpdateRequest updateRequest) throws ClientException
try {
json = objectMapper.writeValueAsString(updateRequestMap);
} catch (JsonProcessingException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.write");
}

Response updateResponse = restCallTimeoutHandler(() -> getClient()
Expand All @@ -193,8 +194,9 @@ public UpdateResponse upsert(UpdateRequest updateRequest) throws ClientException
try {
responseNode = objectMapper.readTree(EntityUtils.toString(updateResponse.getEntity()));
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.read");
}

String id = responseNode.get(ElasticsearchKeywords.KEY_DOC_ID).asText();
String index = responseNode.get(ElasticsearchKeywords.KEY_DOC_INDEX).asText();
String type = responseNode.get(ElasticsearchKeywords.KEY_DOC_TYPE).asText();
Expand All @@ -221,7 +223,7 @@ public BulkUpdateResponse upsert(BulkUpdateRequest bulkUpdateRequest) throws Cli
try {
bulkOperation.append(objectMapper.writeValueAsString(storableMap));
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.write");
}
bulkOperation.append(", \"doc_as_upsert\": true }\n");
}
Expand All @@ -243,7 +245,7 @@ public BulkUpdateResponse upsert(BulkUpdateRequest bulkUpdateRequest) throws Cli
try {
responseNode = objectMapper.readTree(EntityUtils.toString(updateResponse.getEntity()));
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.read");
}

ArrayNode items = (ArrayNode) responseNode.get(ElasticsearchKeywords.KEY_ITEMS);
Expand Down Expand Up @@ -272,7 +274,7 @@ public BulkUpdateResponse upsert(BulkUpdateRequest bulkUpdateRequest) throws Cli
bulkResponse.add(new UpdateResponse(metricId, new TypeDescriptor(indexName, typeName)));
LOG.debug("Upsert on channel metric successfully executed [{}.{}, {}]", indexName, typeName, metricId);
} else {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, "Unexpected action response");
throw new ClientInternalError("Empty JSON response from upsert");
}
}
return bulkResponse;
Expand Down Expand Up @@ -312,8 +314,9 @@ public <T> ResultList<T> query(TypeDescriptor typeDescriptor, Object query, Clas
try {
responseNode = objectMapper.readTree(EntityUtils.toString(queryResponse.getEntity()));
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.read");
}

JsonNode hitsNode = responseNode.get(ElasticsearchKeywords.KEY_HITS);
totalCount = hitsNode.get(ElasticsearchKeywords.KEY_TOTAL).asLong();
if (totalCount > Integer.MAX_VALUE) {
Expand Down Expand Up @@ -365,8 +368,9 @@ public long count(TypeDescriptor typeDescriptor, Object query) throws ClientExce
try {
responseNode = objectMapper.readTree(EntityUtils.toString(queryResponse.getEntity()));
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
throw new ClientInternalError(e, "Error managing ObjectMapper.read");
}

JsonNode hitsNode = responseNode.get(ElasticsearchKeywords.KEY_HITS);
totalCount = hitsNode.get(ElasticsearchKeywords.KEY_TOTAL).asLong();
if (totalCount > Integer.MAX_VALUE) {
Expand Down Expand Up @@ -459,7 +463,7 @@ public IndexResponse findIndexes(IndexRequest indexRequest) throws ClientExcepti
try {
return new IndexResponse(EntityUtils.toString(isIndexExistsResponse.getEntity()).split("\n"));
} catch (ParseException | IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e, "Cannot convert the indexes list");
throw new ClientInternalError(e, "Cannot convert the indexes list");
}
} else if (isIndexExistsResponse.getStatusLine().getStatusCode() == 404) {
return new IndexResponse(null);
Expand Down Expand Up @@ -628,10 +632,8 @@ private <T> T restCallTimeoutHandler(Callable<T> restAction, String index, Strin
} else {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, re);
}
} catch (IOException e) {
throw new ClientException(ClientErrorCodes.ACTION_ERROR, e);
} catch (Exception e) {
throw new ClientInternalError(e);
throw new ClientInternalError(e, "Error in handling REST timeout handler");
}
timeoutRetryLimitReachedCount.inc();

Expand Down

0 comments on commit 39435a7

Please sign in to comment.