use of org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus in project elasticsearch by elastic.
the class RestHighLevelClientTests method testPerformRequestOnResponseExceptionWithEntity.
public void testPerformRequestOnResponseExceptionWithEntity() throws IOException {
MainRequest mainRequest = new MainRequest();
CheckedFunction<MainRequest, Request, IOException> requestConverter = request -> new Request("GET", "/", Collections.emptyMap(), null);
RestStatus restStatus = randomFrom(RestStatus.values());
HttpResponse httpResponse = new BasicHttpResponse(newStatusLine(restStatus));
httpResponse.setEntity(new StringEntity("{\"error\":\"test error message\",\"status\":" + restStatus.getStatus() + "}", ContentType.APPLICATION_JSON));
Response mockResponse = new Response(REQUEST_LINE, new HttpHost("localhost", 9200), httpResponse);
ResponseException responseException = new ResponseException(mockResponse);
when(restClient.performRequest(anyString(), anyString(), anyMapOf(String.class, String.class), anyObject(), anyVararg())).thenThrow(responseException);
ElasticsearchException elasticsearchException = expectThrows(ElasticsearchException.class, () -> restHighLevelClient.performRequest(mainRequest, requestConverter, response -> response.getStatusLine().getStatusCode(), Collections.emptySet()));
assertEquals("Elasticsearch exception [type=exception, reason=test error message]", elasticsearchException.getMessage());
assertEquals(restStatus, elasticsearchException.status());
assertSame(responseException, elasticsearchException.getSuppressed()[0]);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus in project crate by crate.
the class SQLExceptions method createSQLActionException.
/**
* Create a {@link SQLActionException} out of a {@link Throwable}.
* If concrete {@link ElasticsearchException} is found, first transform it
* to a {@link CrateException}
*/
public static SQLActionException createSQLActionException(Throwable e) {
if (e instanceof SQLActionException) {
return (SQLActionException) e;
}
e = esToCrateException(e);
int errorCode = 5000;
RestStatus restStatus = RestStatus.INTERNAL_SERVER_ERROR;
if (e instanceof CrateException) {
CrateException crateException = (CrateException) e;
if (e instanceof ValidationException) {
errorCode = 4000 + crateException.errorCode();
restStatus = RestStatus.BAD_REQUEST;
} else if (e instanceof ReadOnlyException) {
errorCode = 4030 + crateException.errorCode();
restStatus = RestStatus.FORBIDDEN;
} else if (e instanceof ResourceUnknownException) {
errorCode = 4040 + crateException.errorCode();
restStatus = RestStatus.NOT_FOUND;
} else if (e instanceof ConflictException) {
errorCode = 4090 + crateException.errorCode();
restStatus = RestStatus.CONFLICT;
} else if (e instanceof UnhandledServerException) {
errorCode = 5000 + crateException.errorCode();
}
} else if (e instanceof ParsingException) {
errorCode = 4000;
restStatus = RestStatus.BAD_REQUEST;
} else if (e instanceof MapperParsingException) {
errorCode = 4000;
restStatus = RestStatus.BAD_REQUEST;
}
String message = e.getMessage();
if (message == null) {
if (e instanceof CrateException && e.getCause() != null) {
// use cause because it contains a more meaningful error in most cases
e = e.getCause();
}
StackTraceElement[] stackTraceElements = e.getStackTrace();
if (stackTraceElements.length > 0) {
message = String.format(Locale.ENGLISH, "%s in %s", e.getClass().getSimpleName(), stackTraceElements[0]);
} else {
message = "Error in " + e.getClass().getSimpleName();
}
} else {
message = e.getClass().getSimpleName() + ": " + message;
}
return new SQLActionException(message, errorCode, restStatus, e.getStackTrace());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus in project nifi by apache.
the class TestDeleteElasticsearch5 method testDeleteSuccessful.
@Test
public void testDeleteSuccessful() throws IOException {
restStatus = RestStatus.OK;
deleteResponse = new DeleteResponse(null, TYPE1, documentId, 1, true) {
@Override
public RestStatus status() {
return restStatus;
}
};
runner.enqueue(new byte[] {}, new HashMap<String, String>() {
{
put("documentId", documentId);
}
});
runner.run(1, true, true);
runner.assertAllFlowFilesTransferred(DeleteElasticsearch5.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(DeleteElasticsearch5.REL_SUCCESS).get(0);
assertNotNull(out);
assertEquals(null, out.getAttribute(DeleteElasticsearch5.ES_ERROR_MESSAGE));
out.assertAttributeEquals(DeleteElasticsearch5.ES_FILENAME, documentId);
out.assertAttributeEquals(DeleteElasticsearch5.ES_INDEX, INDEX1);
out.assertAttributeEquals(DeleteElasticsearch5.ES_TYPE, TYPE1);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus in project baseio by generallycloud.
the class TestPut method test.
@SuppressWarnings("resource")
public static void test() throws Exception {
Settings esSettings = Settings.builder().build();
/*
这里的连接方式指的是没有安装x-pack插件,如果安装了x-pack则参考{@link ElasticsearchXPackClient}
1. java客户端的方式是以tcp协议在9300端口上进行通信
2. http客户端的方式是以http协议在9200端口上进行通信
*/
TransportClient client = new PreBuiltTransportClient(esSettings).addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
System.out.println("ElasticsearchClient 连接成功");
String index = "twitter";
IndexResponse putResponse = client.prepareIndex(index, "tweet", "1").setSource(jsonBuilder().startObject().field("user", "kimchy").field("postDate", new Date()).field("message", "trying out Elasticsearch").endObject()).get();
// Index name
String _index = putResponse.getIndex();
// Type name
String _type = putResponse.getType();
// Document ID (generated or not)
String _id = putResponse.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = putResponse.getVersion();
// status has stored current instance statement.
RestStatus status = putResponse.status();
System.out.println(_index);
System.out.println(_type);
System.out.println(_id);
System.out.println(_version);
System.out.println(status);
GetRequest getRequest = new GetRequest("twitter", _type, _id);
GetResponse getResponse = client.get(getRequest).get();
System.out.println(getResponse.getSource());
client.close();
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.rest.RestStatus in project flink by apache.
the class ElasticsearchWriter method extractFailures.
private void extractFailures(BulkRequest request, BulkResponse response) {
if (!response.hasFailures()) {
pendingActions -= request.numberOfActions();
return;
}
Throwable chainedFailures = null;
for (int i = 0; i < response.getItems().length; i++) {
final BulkItemResponse itemResponse = response.getItems()[i];
if (!itemResponse.isFailed()) {
continue;
}
final Throwable failure = itemResponse.getFailure().getCause();
if (failure == null) {
continue;
}
final RestStatus restStatus = itemResponse.getFailure().getStatus();
final DocWriteRequest<?> actionRequest = request.requests().get(i);
chainedFailures = firstOrSuppressed(wrapException(restStatus, failure, actionRequest), chainedFailures);
}
if (chainedFailures == null) {
return;
}
throw new FlinkRuntimeException(chainedFailures);
}
Aggregations