# Java Client - Error Handling

**URL:** <https://discuss.elastic.co/t/java-client-error-handling/17926>\
**Category:** Elasticsearch\
**Created:** [June 5, 2014, 11:29am UTC](https://discuss.elastic.co/t/java-client-error-handling/17926 "2014-06-05T11:29:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nir\_Dothan](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nir\_Dothan](https://discuss.elastic.co/u/Nir_Dothan)\
**Post date:** [June 5, 2014, 11:29am UTC](https://discuss.elastic.co/t/java-client-error-handling/17926/1 "2014-06-05T11:29:57Z")

</div>

I'm not sure how to handle errors when using the java client. How do I  
grammatically know if my connection was successful, or if indexing of a  
document succeeded?  
In Rest we have the http result code, but in java, I did not see a  
documented way to catch checked exceptions or anything like that.  
For connection assurance I use:  
return 0\< client.admin().cluster().prepareState().get().getState().  
getVersion();  
Is that correct? is it effective?  
Can anyone please refer my to any documentation or relevant source code?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [June 5, 2014, 12:44pm UTC](https://discuss.elastic.co/t/java-client-error-handling/17926/2 "2014-06-05T12:44:09Z")

</div>

Do you use TransportClient or NodeClient?

On NodeClient, you are tied to the cluster, as the node is being a part of  
it, on TransportClient, you can count the connected nodes.

The discovery mechanism behind the scenes sends "ping" actions each few  
seconds for you. If an action fails, you will see exceptions there. No need  
to ask explicitly the cluster for a state or something (your code also just  
asks for the local state copy which has nothing to do with testing  
connections)

Jörg

On Thu, Jun 5, 2014 at 1:29 PM, Nir Dothan [nir.dothan@gmail.com](mailto:nir.dothan@gmail.com) wrote:

> I'm not sure how to handle errors when using the java client. How do I  
> grammatically know if my connection was successful, or if indexing of a  
> document succeeded?  
> In Rest we have the http result code, but in java, I did not see a  
> documented way to catch checked exceptions or anything like that.  
> For connection assurance I use:  
> return 0\< client.admin().cluster().prepareState().get().getState().  
> getVersion();  
> Is that correct? is it effective?  
> Can anyone please refer my to any documentation or relevant source code?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoG3HfOkDYbLqrwhT%3DHBXB1ipZwKtg3mDQbyJ3%3D4ZP0YFg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoG3HfOkDYbLqrwhT%3DHBXB1ipZwKtg3mDQbyJ3%3D4ZP0YFg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Nir\_Dothan](https://avatars.discourse-cdn.com/v4/letter/n/a587f6/32.png) [@Nir\_Dothan](https://discuss.elastic.co/u/Nir_Dothan)\
**Post date:** [June 5, 2014, 2:29pm UTC](https://discuss.elastic.co/t/java-client-error-handling/17926/3 "2014-06-05T14:29:08Z")

</div>

Thanks. The code I'm developing will support both Node and Transport  
clients. The selection will be configuration driven.  
There must be a way to determine if a CRUD operation succeeded. For  
example, see the following code taken from the Logstash Ruby client based  
plugin. Is there any Java client equivalent?

def template\_exists?(name)  
@client.indices.get\_template(:name =\> name)  
return true  
rescue Elasticsearch::Transport::Transport::Errors::NotFound  
return false  
end # def template\_exists?

In addition, for transport client do you recommend checking  
client.connectedNodes().size() \> 0 ?

On Thursday, June 5, 2014 3:44:25 PM UTC+3, Jörg Prante wrote:

> Do you use TransportClient or NodeClient?
> 
> On NodeClient, you are tied to the cluster, as the node is being a part of  
> it, on TransportClient, you can count the connected nodes.
> 
> The discovery mechanism behind the scenes sends "ping" actions each few  
> seconds for you. If an action fails, you will see exceptions there. No need  
> to ask explicitly the cluster for a state or something (your code also just  
> asks for the local state copy which has nothing to do with testing  
> connections)
> 
> Jörg
> 
> On Thu, Jun 5, 2014 at 1:29 PM, Nir Dothan \<[nir.d...@gmail.com](mailto:nir.d...@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > I'm not sure how to handle errors when using the java client. How do I  
> > grammatically know if my connection was successful, or if indexing of a  
> > document succeeded?  
> > In Rest we have the http result code, but in java, I did not see a  
> > documented way to catch checked exceptions or anything like that.  
> > For connection assurance I use:  
> > return 0\< client.admin().cluster().prepareState().get().getState().  
> > getVersion();  
> > Is that correct? is it effective?  
> > Can anyone please refer my to any documentation or relevant source code?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5f4a13ee-25e2-40f4-8163-b19a718fa225%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f4a13ee-25e2-40f4-8163-b19a718fa225%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [June 5, 2014, 3:41pm UTC](https://discuss.elastic.co/t/java-client-error-handling/17926/4 "2014-06-05T15:41:21Z")

</div>

Check the Elasticsearch test code. There, you can see how Java API works.

For example

GetIndexTemplatesResponse response =  
client().admin().indices().prepareGetTemplates().get();

You can get an empty response if template does not exist, or the execution  
throws an exception, when something went wrong.

After initialization of TransportClient, you could check connectedNodes(),  
yes. While running, you will get an exception if no node is connected, and  
you execute an action.

Jörg

On Thu, Jun 5, 2014 at 4:29 PM, Nir Dothan [nir.dothan@gmail.com](mailto:nir.dothan@gmail.com) wrote:

> Thanks. The code I'm developing will support both Node and Transport  
> clients. The selection will be configuration driven.  
> There must be a way to determine if a CRUD operation succeeded. For  
> example, see the following code taken from the Logstash Ruby client based  
> plugin. Is there any Java client equivalent?
> 
> def template\_exists?(name)  
> @client.indices.get\_template(:name =\> name)  
> return true  
> rescue Elasticsearch::Transport::Transport::Errors::NotFound  
> return false  
> end # def template\_exists?
> 
> In addition, for transport client do you recommend checking  
> client.connectedNodes().size() \> 0 ?
> 
> On Thursday, June 5, 2014 3:44:25 PM UTC+3, Jörg Prante wrote:
> 
> > Do you use TransportClient or NodeClient?
> > 
> > On NodeClient, you are tied to the cluster, as the node is being a part  
> > of it, on TransportClient, you can count the connected nodes.
> > 
> > The discovery mechanism behind the scenes sends "ping" actions each few  
> > seconds for you. If an action fails, you will see exceptions there. No need  
> > to ask explicitly the cluster for a state or something (your code also just  
> > asks for the local state copy which has nothing to do with testing  
> > connections)
> > 
> > Jörg
> > 
> > On Thu, Jun 5, 2014 at 1:29 PM, Nir Dothan [nir.d...@gmail.com](mailto:nir.d...@gmail.com) wrote:
> > 
> > > I'm not sure how to handle errors when using the java client. How do I  
> > > grammatically know if my connection was successful, or if indexing of a  
> > > document succeeded?  
> > > In Rest we have the http result code, but in java, I did not see a  
> > > documented way to catch checked exceptions or anything like that.  
> > > For connection assurance I use:  
> > > return 0\< client.admin().cluster().prepareState().get().getState().get  
> > > Version();  
> > > Is that correct? is it effective?  
> > > Can anyone please refer my to any documentation or relevant source code?
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).
> > > 
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%  
> > > [40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f98a54e0-efb0-47f8-a391-e2ec3920a3d1%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/5f4a13ee-25e2-40f4-8163-b19a718fa225%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f4a13ee-25e2-40f4-8163-b19a718fa225%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/5f4a13ee-25e2-40f4-8163-b19a718fa225%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5f4a13ee-25e2-40f4-8163-b19a718fa225%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFgnUwZTb%3DGtL1iOcYnjYLMh80bD2NG4CaiW3GiVM868w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFgnUwZTb%3DGtL1iOcYnjYLMh80bD2NG4CaiW3GiVM868w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:24am UTC](https://discuss.elastic.co/t/java-client-error-handling/17926/5 "2017-07-06T01:24:27Z")

</div>


