Skip to content

Commit

Permalink
Merge pull request #80 from victor-perov/victor-perov/return-error-of…
Browse files Browse the repository at this point in the history
…-failed-connection

Return original error when connection to ClickHouse failed
  • Loading branch information
DoubleDi committed Aug 6, 2019
2 parents d5cdd1b + 0d72e2a commit 0c89049
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
13 changes: 8 additions & 5 deletions conn_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
// Ping implements the driver.Pinger
func (c *conn) Ping(ctx context.Context) error {
if c.transport == nil {
return driver.ErrBadConn
return ErrTransportNil
}
// make request with empty body, response should be "Ok"
// make request with empty body, response must be "Ok"
u := &url.URL{Scheme: c.url.Scheme, User: c.url.User, Host: c.url.Host, Path: "/"}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
Expand All @@ -27,11 +27,14 @@ func (c *conn) Ping(ctx context.Context) error {
c.cancel = nil
}()
if err != nil {
return driver.ErrBadConn
return err
}
resp, err := ioutil.ReadAll(respBody)
if err != nil || len(resp) != 4 || !strings.HasPrefix(string(resp), "Ok.") {
return driver.ErrBadConn
if err != nil {
return err
}
if len(resp) != 4 || !strings.HasPrefix(string(resp), "Ok.") {
return ErrIncorrectResponse
}
return nil
}
Expand Down
12 changes: 7 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

// Various errors the driver might return. Can change between driver versions.
var (
ErrPlaceholderCount = errors.New("clickhouse: wrong placeholder count")
ErrNameParams = errors.New("clickhouse: driver does not support the use of Named Parameters")
ErrMalformed = errors.New("clickhouse: response is malformed")
ErrNoLastInsertID = errors.New("no LastInsertId available")
ErrNoRowsAffected = errors.New("no RowsAffected available")
ErrPlaceholderCount = errors.New("clickhouse: wrong placeholder count")
ErrNameParams = errors.New("clickhouse: driver does not support the use of Named Parameters")
ErrMalformed = errors.New("clickhouse: response is malformed")
ErrTransportNil = errors.New("clickhouse: transport must be set")
ErrIncorrectResponse = errors.New("clickhouse: response must contain 'Ok.'")
ErrNoLastInsertID = errors.New("no LastInsertId available")
ErrNoRowsAffected = errors.New("no RowsAffected available")
)

var errorRe = regexp.MustCompile(`(?s)Code: (\d+),.+DB::Exception: (.+),.*`)
Expand Down

0 comments on commit 0c89049

Please sign in to comment.