Ignoring exceptions
editIgnoring exceptions
editThe library attempts to throw exceptions for common problems. These exceptions match the HTTP response code provided
by Elasticsearch. For example, attempting to GET a nonexistent document will throw a MissingDocument404Exception
.
Exceptions are a useful and consistent way to deal with problems like missing documents, syntax errors, version conflicts, etc. But sometimes you want to deal with the response body rather than catch exceptions (often useful in test suites).
If you need that behavior, you can configure an ignore
parameter. This should be configured in the client
parameter
of the reuqest array. For example, this example will ignore the MissingDocument404Exception
exception and instead return the JSON provided by Elasticsearch.
$client = ClientBuilder::create()->build(); $params = [ 'index' => 'test_missing', 'type' => 'test', 'id' => 1, 'client' => [ 'ignore' => 404 ] ]; echo $client->get($params); > {"_index":"test_missing","_type":"test","_id":"1","found":false}
You can specify multiple HTTP status codes to ignore, by providing an array of values:
$client = ClientBuilder::create()->build(); $params = [ 'index' => 'test_missing', 'type' => 'test', 'client' => [ 'ignore' => [400, 404] ] ]; echo $client->get($params); > No handler found for uri [/test_missing/test/] and method [GET]
|
It should be noted that the response is simply a string, which may or may not be encoded as JSON. In the first example, the response body was a complete JSON object which could be decoded. In the second example, it was simply a string.
Since the client has no way of knowing what the exception response will contain, no attempts to decode it are taken.