aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/clickhouse-go/clickhouse_exception.go
blob: 2317d94377103d2945e0475f135254b992ff1600 (plain) (blame)
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
package clickhouse

import (
	"fmt"
	"strings"
)

type Exception struct {
	Code       int32
	Name       string
	Message    string
	StackTrace string
	nested     error
}

func (e *Exception) Error() string {
	return fmt.Sprintf("code: %d, message: %s", e.Code, e.Message)
}

func (ch *clickhouse) exception() error {
	var (
		e         Exception
		err       error
		hasNested bool
	)
	if e.Code, err = ch.decoder.Int32(); err != nil {
		return err
	}
	if e.Name, err = ch.decoder.String(); err != nil {
		return err
	}
	if e.Message, err = ch.decoder.String(); err != nil {
		return err
	}
	e.Message = strings.TrimSpace(strings.TrimPrefix(e.Message, e.Name+":"))
	if e.StackTrace, err = ch.decoder.String(); err != nil {
		return err
	}
	if hasNested, err = ch.decoder.Bool(); err != nil {
		return err
	}
	if hasNested {
		e.nested = ch.exception()
	}
	return &e
}