-
Notifications
You must be signed in to change notification settings - Fork 104
/
error_analytics.go
92 lines (83 loc) · 3.16 KB
/
error_analytics.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
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
package gocb
import (
"encoding/json"
gocbcore "github.com/couchbase/gocbcore/v10"
)
// AnalyticsErrorDesc represents a specific error returned from the analytics service.
type AnalyticsErrorDesc struct {
Code uint32
Message string
}
func translateCoreAnalyticsErrorDesc(descs []gocbcore.AnalyticsErrorDesc) []AnalyticsErrorDesc {
descsOut := make([]AnalyticsErrorDesc, len(descs))
for descIdx, desc := range descs {
descsOut[descIdx] = AnalyticsErrorDesc{
Code: desc.Code,
Message: desc.Message,
}
}
return descsOut
}
// AnalyticsError is the error type of all analytics query errors.
// UNCOMMITTED: This API may change in the future.
type AnalyticsError struct {
InnerError error `json:"-"`
Statement string `json:"statement,omitempty"`
ClientContextID string `json:"client_context_id,omitempty"`
Errors []AnalyticsErrorDesc `json:"errors,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
RetryReasons []RetryReason `json:"retry_reasons,omitempty"`
RetryAttempts uint32 `json:"retry_attempts,omitempty"`
}
// MarshalJSON implements the Marshaler interface.
func (e AnalyticsError) MarshalJSON() ([]byte, error) {
var innerError string
if e.InnerError != nil {
innerError = e.InnerError.Error()
}
return json.Marshal(struct {
InnerError string `json:"msg,omitempty"`
Statement string `json:"statement,omitempty"`
ClientContextID string `json:"client_context_id,omitempty"`
Errors []AnalyticsErrorDesc `json:"errors,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
RetryReasons []RetryReason `json:"retry_reasons,omitempty"`
RetryAttempts uint32 `json:"retry_attempts,omitempty"`
}{
InnerError: innerError,
Statement: e.Statement,
ClientContextID: e.ClientContextID,
Errors: e.Errors,
Endpoint: e.Endpoint,
RetryReasons: e.RetryReasons,
RetryAttempts: e.RetryAttempts,
})
}
// Error returns the string representation of this error.
func (e AnalyticsError) Error() string {
errBytes, serErr := json.Marshal(struct {
InnerError error `json:"-"`
Statement string `json:"statement,omitempty"`
ClientContextID string `json:"client_context_id,omitempty"`
Errors []AnalyticsErrorDesc `json:"errors,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
RetryReasons []RetryReason `json:"retry_reasons,omitempty"`
RetryAttempts uint32 `json:"retry_attempts,omitempty"`
}{
InnerError: e.InnerError,
Statement: e.Statement,
ClientContextID: e.ClientContextID,
Errors: e.Errors,
Endpoint: e.Endpoint,
RetryReasons: e.RetryReasons,
RetryAttempts: e.RetryAttempts,
})
if serErr != nil {
logErrorf("failed to serialize error to json: %s", serErr.Error())
}
return e.InnerError.Error() + " | " + string(errBytes)
}
// Unwrap returns the underlying cause for this error.
func (e AnalyticsError) Unwrap() error {
return e.InnerError
}