Handle specific type of ElasticsearchException

Hi Team,
We are facing a very rare scenario, in our cluster deployment we have used dependent services in which the ES repository bean which creates an index is autowired in other service bean, so on startup index creation is called by lets say A(repository) and during its creation B(service) is getting loaded which is also calling index creation, while we do have check for index_exists before doing create request but at some times the call from B will go to server even before A has loaded mapping or settings allowing it to go ahead with B index creation and when B starts index creation it fails with ElasticsearchException resource_already_exists, which is correct way but how are we suppose to handle this specific type of exception as ElasticsearchException is quite generic and we only want to handle it when the subtype is resource_already_exists

Yeah, using a dependent services always results to a conflict when multiple services are also implemented. You can try using a try-catch statement. In the "try" block, you can call the index creation method. In the "catch" block, you can check the type of exception and handle it accordingly.

Assuming you're using Java (because of "repository bean"). Would testing the error type work?

try {
    doSomething();
} catch (ElasticsearchException e) {
    if (e.error().type().equals("resource_already_exists_exception")) {
        handleAlreadyExists();
    } else {
        throw e;
    }
}
1 Like

Thanks @swallez, for quick response.

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