1package clickhouse
2
3import (
4	"fmt"
5	"strings"
6)
7
8type Exception struct {
9	Code       int32
10	Name       string
11	Message    string
12	StackTrace string
13	nested     error
14}
15
16func (e *Exception) Error() string {
17	return fmt.Sprintf("code: %d, message: %s", e.Code, e.Message)
18}
19
20func (ch *clickhouse) exception() error {
21	defer ch.conn.Close()
22	var (
23		e         Exception
24		err       error
25		hasNested bool
26	)
27	if e.Code, err = ch.decoder.Int32(); err != nil {
28		return err
29	}
30	if e.Name, err = ch.decoder.String(); err != nil {
31		return err
32	}
33	if e.Message, err = ch.decoder.String(); err != nil {
34		return err
35	}
36	e.Message = strings.TrimSpace(strings.TrimPrefix(e.Message, e.Name+":"))
37	if e.StackTrace, err = ch.decoder.String(); err != nil {
38		return err
39	}
40	if hasNested, err = ch.decoder.Bool(); err != nil {
41		return err
42	}
43	if hasNested {
44		e.nested = ch.exception()
45	}
46	return &e
47}
48