How to handle ElasticSearch RestHighLevelClient exceptions properly?


#1

What is the proper way to handle Exceptions when dealing with the ElasticSearch RestHighLevelClient introduced in 5.0? I am currently working with 6.1.

There have been some discussions on whether to use checked or unchecked exceptions (https://github.com/elastic/elasticsearch/issues/19980) but it seems that the RestHighLevelClient still throws unchecked ElasticsearchExceptions. ElasticsearchException has many different subclasses and is a direct subclass of RuntimeException itself.

However, most straightforward methods like client.search(..) can also throw the checked exception IOException. ElasticSearch implements ResponseException as a subclass of IOException which is thrown, for example, in the org.elasticsearch.client.RestClient.

I figured, I'd catch this IOException to handle any ES error gracefully and be done with it, but this seems to be incorrect. During some invalid query (value for integer too large) this ResponseException is thrown, but somewhere inside ElasticSearch client converted to an unchecked ElasticsearchException which I didn't catch yet!

What is even worse, none of this exception handling seems to be documented in the otherwise excellent ES documentation: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-search.html

Right now, I basically have to write my own wrapper to handle all realistic exceptions, and do this for every client method I wish to use in my application. For example, this is my wrapper for search, the most common use case.

public SearchResponse search(String[] indexNames, String docType, SearchSourceBuilder source) throws MyESException {
	try {
		return client.search(new SearchRequest(indexNames).types(docType).source(source));
	} catch (IOException | ElasticsearchException e) {
		throw new MyESException(e);
	}
}

As you can see, this is quite ugly, having to handle the checked IOException and unchecked ElasticsearchException identically. I can't imagine this is the intended way for the Java API to be used like this. Am I overlooking some crucial part of the ES documentation that explains this in greater detail?


(Luca Cavanna) #2

See opened issue: https://github.com/elastic/elasticsearch/issues/30334 . We are currently discussing this there.


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.