-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy patherrors_columnar.go
More file actions
118 lines (105 loc) · 3.49 KB
/
errors_columnar.go
File metadata and controls
118 lines (105 loc) · 3.49 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package gocbcore
import "encoding/json"
// ColumnarErrorDesc represents specific Columnar error data.
type ColumnarErrorDesc struct {
Code uint32
Message string
Retry bool
}
// MarshalJSON implements the Marshaler interface.
func (e ColumnarErrorDesc) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Code uint32 `json:"code"`
Message string `json:"msg"`
}{
Code: e.Code,
Message: e.Message,
})
}
// ColumnarError represents an error returned from an Columnar query.
type ColumnarError struct {
InnerError error
Statement string
Errors []ColumnarErrorDesc
LastErrorCode uint32
LastErrorMsg string
Endpoint string
ErrorText string
HTTPResponseCode int
WasNotDispatched bool
}
func newColumnarError(innerError error, statement string, endpoint string, responseCode int) *ColumnarError {
return &ColumnarError{
InnerError: innerError,
Statement: statement,
Endpoint: endpoint,
HTTPResponseCode: responseCode,
}
}
func (e ColumnarError) withErrorText(errText string) *ColumnarError {
e.ErrorText = errText
return &e
}
func (e ColumnarError) withLastDetail(code uint32, msg string) *ColumnarError {
e.LastErrorCode = code
e.LastErrorMsg = msg
return &e
}
func (e ColumnarError) withErrors(errors []ColumnarErrorDesc) *ColumnarError {
e.Errors = errors
return &e
}
func (e ColumnarError) withWasNotDispatched() *ColumnarError {
e.WasNotDispatched = true
return &e
}
// MarshalJSON implements the Marshaler interface.
func (e ColumnarError) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
InnerError string `json:"msg,omitempty"`
Statement string `json:"statement,omitempty"`
Errors []ColumnarErrorDesc `json:"errors,omitempty"`
LastCode uint32 `json:"lastCode,omitempty"`
LastMessage string `json:"lastMsg,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
HTTPResponseCode int `json:"status_code,omitempty"`
}{
InnerError: e.InnerError.Error(),
Statement: e.Statement,
Errors: e.Errors,
LastCode: e.LastErrorCode,
LastMessage: e.LastErrorMsg,
Endpoint: e.Endpoint,
HTTPResponseCode: e.HTTPResponseCode,
})
}
// Error returns the string representation of this error.
func (e ColumnarError) Error() string {
errBytes, serErr := json.Marshal(struct {
InnerError error `json:"-"`
Statement string `json:"statement,omitempty"`
Errors []ColumnarErrorDesc `json:"errors,omitempty"`
LastCode uint32 `json:"lastCode,omitempty"`
LastMessage string `json:"lastMsg,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
ErrorText string `json:"error_text,omitempty"`
HTTPResponseCode int `json:"status_code,omitempty"`
}{
InnerError: e.InnerError,
Statement: e.Statement,
Errors: e.Errors,
LastCode: e.LastErrorCode,
LastMessage: e.LastErrorMsg,
Endpoint: e.Endpoint,
ErrorText: e.ErrorText,
HTTPResponseCode: e.HTTPResponseCode,
})
if serErr != nil {
logErrorf("failed to serialize error to json: %s", serErr.Error())
}
return e.InnerError.Error() + " | " + string(errBytes)
}
// Unwrap returns the underlying reason for the error
func (e ColumnarError) Unwrap() error {
return e.InnerError
}