forked from mintance/go-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
57 lines (48 loc) · 996 Bytes
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package clickhouse
import (
"fmt"
"strconv"
"strings"
)
type DbError struct {
code int
msg string
resp string
}
func (e *DbError) Code() int {
return e.code
}
func (e *DbError) Message() string {
return e.msg
}
func (e *DbError) Response() string {
return e.resp
}
func (e *DbError) Error() string {
return fmt.Sprintf("clickhouse error: [%d] %s", e.code, e.msg)
}
func (e *DbError) String() string {
return fmt.Sprintf("[error code=%d message=%q]", e.code, e.msg)
}
func errorFromResponse(resp string) error {
if resp == "" {
return nil
}
if strings.Index(resp, "Code:") == 0 {
codeStr := resp[6:strings.Index(resp, ",")]
code, _ := strconv.Atoi(codeStr)
var msg string
msgIndex := strings.Index(resp, "e.displayText() = ")
if msgIndex >= 0 {
msgIndex += 18
msgEnd := strings.Index(resp, ", e.what()")
if msgEnd >= 0 {
msg = resp[msgIndex:msgEnd]
} else {
msg = resp[msgIndex:]
}
}
return &DbError{code, msg, resp}
}
return nil
}