Skip to content

Commit

Permalink
added parsing response body and noticing errors in exec requests
Browse files Browse the repository at this point in the history
  • Loading branch information
xtreding committed Dec 11, 2020
1 parent e482166 commit d4bfcd4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,15 @@ func (c *conn) exec(ctx context.Context, query string, args []driver.Value) (dri
return nil, err
}
body, err := c.doRequest(ctx, req)
if body != nil {
body.Close()
if err != nil {
if body != nil {
body.Close()
}

return emptyResult, err
}
return emptyResult, err

return emptyResult, noticeError(body)
}

func (c *conn) doRequest(ctx context.Context, req *http.Request) (io.ReadCloser, error) {
Expand Down
19 changes: 19 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse

import (
"bytes"
"io"
"net/http"
"strings"
"time"
Expand All @@ -11,6 +12,8 @@ var (
escaper = strings.NewReplacer(`\`, `\\`, `'`, `\'`)
dateFormat = "2006-01-02"
timeFormat = "2006-01-02 15:04:05"

exceptionMarker = "DB::Exception"
)

func escape(s string) string {
Expand Down Expand Up @@ -40,6 +43,22 @@ func readResponse(response *http.Response) (result []byte, err error) {
return
}

func noticeError(body io.ReadCloser) (err error) {
if body == nil {
return nil
}

buf := new(bytes.Buffer)
defer body.Close()
_, err = buf.ReadFrom(body)
msg := buf.String()
if strings.Contains(msg, exceptionMarker) {
return newError(msg)
}

return nil
}

func numOfColumns(data []byte) int {
var cnt int
for _, ch := range data {
Expand Down

0 comments on commit d4bfcd4

Please sign in to comment.