Skip to content
Oliver Eilhard edited this page May 6, 2017 · 7 revisions

Errors are returned with the standard Go mechanism. However, if you need to get the details of the error as reported by Elasticsearch, you can do so by "casting" to *elastic.Error. Here's an example:

_, err := client.IndexExists("twitter").Do()
if err != nil {
    // Get *elastic.Error which contains additional information
    e, ok := err.(*elastic.Error)
    if !ok {
        // This shouldn't happen
        ...
    }
    log.Printf("Elastic failed with status %d and error %s.", e.Status, e.Details)
    ...
}

Wrapping errors (and the IsConnErr helper)

Starting from 5.0.37, we use github.com/pkg/errors to wrap errors. This enhances error messages as it no longer hides the underlying problem, but concatenates the error with the "wrapped" error. Unfortunately, an equality check for a specific error might now fail. The most common situation you are looking for is to check for a connection error. Do not try to compare err == elastic.ErrNoClient. Instead, use the IsConnErr helper which you can use like so:

if elastic.IsConnErr(err) {
    log.Fatalf("Elasticsearch connection problem: %v", err)
}
Clone this wiki locally