Skip to content

Commit

Permalink
Add a helper function for checking client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
eticzon committed Apr 27, 2017
1 parent a593d23 commit 318aabe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1717,3 +1717,9 @@ func (c *Client) WaitForGreenStatus(timeout string) error {
func (c *Client) WaitForYellowStatus(timeout string) error {
return c.WaitForStatus("yellow", timeout)
}

// IsConnError unwraps the given error value and checks if it is equal to
// elastic.ErrNoClient.
func IsConnErr(err error) bool {
return errors.Cause(err) == ErrNoClient
}
9 changes: 4 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"testing"
"time"

"github.com/pkg/errors"
)

func findConn(s string, slice ...*conn) (int, bool) {
Expand Down Expand Up @@ -272,7 +271,7 @@ func TestClientHealthcheckStartupTimeout(t *testing.T) {
start := time.Now()
_, err := NewClient(SetURL("http://localhost:9299"), SetHealthcheckTimeoutStartup(5*time.Second))
duration := time.Now().Sub(start)
if errors.Cause(err) != ErrNoClient {
if !IsConnErr(err) {
t.Fatal(err)
}
if duration < 5*time.Second {
Expand Down Expand Up @@ -648,9 +647,9 @@ func TestClientSelectConnAllDead(t *testing.T) {
client.conns[1].MarkAsDead()

// If all connections are dead, next should make them alive again, but
// still return the wrapped error value ErrNoClient when it first finds out.
// still return an error when it first finds out.
c, err := client.next()
if errors.Cause(err) != ErrNoClient {
if !IsConnErr(err) {
t.Fatal(err)
}
if c != nil {
Expand Down

0 comments on commit 318aabe

Please sign in to comment.