public void testWrapExceptionToPreserveStatus() throws IOException { Exception cause = new Exception(); // Successfully get the status without a body RestStatus status = randomFrom(RestStatus.values()); ElasticsearchStatusException wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause); assertEquals(status, wrapped.status()); assertEquals(cause, wrapped.getCause()); assertEquals("No error body.", wrapped.getMessage()); // Successfully get the status without a body HttpEntity okEntity = new StringEntity("test body", StandardCharsets.UTF_8); wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus( status.getStatus(), okEntity, cause); assertEquals(status, wrapped.status()); assertEquals(cause, wrapped.getCause()); assertEquals("body=test body", wrapped.getMessage()); // Successfully get the status with a broken body IOException badEntityException = new IOException(); HttpEntity badEntity = mock(HttpEntity.class); when(badEntity.getContent()).thenThrow(badEntityException); wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus( status.getStatus(), badEntity, cause); assertEquals(status, wrapped.status()); assertEquals(cause, wrapped.getCause()); assertEquals("Failed to extract body.", wrapped.getMessage()); assertEquals(badEntityException, wrapped.getSuppressed()[0]); // Fail to get the status without a body int notAnHttpStatus = -1; assertNull(RestStatus.fromCode(notAnHttpStatus)); wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause); assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status()); assertEquals(cause, wrapped.getCause()); assertEquals( "Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage()); // Fail to get the status without a body wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause); assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status()); assertEquals(cause, wrapped.getCause()); assertEquals( "Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage()); // Fail to get the status with a broken body wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause); assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status()); assertEquals(cause, wrapped.getCause()); assertEquals( "Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.", wrapped.getMessage()); assertEquals(badEntityException, wrapped.getSuppressed()[0]); }
public RestStatus status() { RestStatus status = RestStatus.OK; for (IndexDeleteByQueryResponse indexResponse : indices.values()) { if (indexResponse.getFailedShards() > 0) { RestStatus indexStatus = indexResponse.getFailures()[0].status(); if (indexResponse.getFailures().length > 1) { for (int i = 1; i < indexResponse.getFailures().length; i++) { if (indexResponse.getFailures()[i].status().getStatus() >= 500) { indexStatus = indexResponse.getFailures()[i].status(); } } } if (status.getStatus() < indexStatus.getStatus()) { status = indexStatus; } } } return status; }
public RestStatus status() { if (shardFailures.length == 0) { if (successfulShards == 0 && totalShards > 0) { return RestStatus.SERVICE_UNAVAILABLE; } return RestStatus.OK; } // if total failure, bubble up the status code to the response level if (successfulShards == 0 && totalShards > 0) { RestStatus status = RestStatus.OK; for (int i = 0; i < shardFailures.length; i++) { RestStatus shardStatus = shardFailures[i].status(); if (shardStatus.getStatus() >= status.getStatus()) { status = shardFailures[i].status(); } } return status; } return RestStatus.OK; }