Ignoring exceptions

edit

The 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. The following setting will ignore the MissingDocument404Exception exception and instead return the JSON provided by Elasticsearch:

$client = new Client();

$params = array(
    'index'  => 'test_missing',
    'type'   => 'test',
    'id'     => 1,
    'ignore' => 404 
);
echo $client->get($params);

> {"_index":"test_missing","_type":"test","_id":"1","found":false}


$params = array(
    'index'  => 'test_missing',
    'type'   => 'test',
    'ignore' => [400, 404] 
);
echo $client->get($params);

> No handler found for uri [/test_missing/test/] and method [GET]

This will ignore just the 404 missing exception

ignore also accepts an array of exceptions to ignore. In this example, the BadRequest400Exception is being ignored

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.