/
error_wrapping.go
153 lines (140 loc) · 5.15 KB
/
error_wrapping.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package gocb
import (
"encoding/json"
"errors"
gocbcore "github.com/couchbase/gocbcore/v10"
)
func serializeWrappedError(err error) string {
errBytes, serErr := json.Marshal(err)
if serErr != nil {
logErrorf("failed to serialize error to json: %s", serErr.Error())
}
return string(errBytes)
}
func maybeEnhanceCoreErr(err error) error {
if kvErr, ok := err.(*gocbcore.KeyValueError); ok {
return &KeyValueError{
InnerError: kvErr.InnerError,
StatusCode: kvErr.StatusCode,
DocumentID: kvErr.DocumentKey,
BucketName: kvErr.BucketName,
ScopeName: kvErr.ScopeName,
CollectionName: kvErr.CollectionName,
CollectionID: kvErr.CollectionID,
ErrorName: kvErr.ErrorName,
ErrorDescription: kvErr.ErrorDescription,
Opaque: kvErr.Opaque,
Context: kvErr.Context,
Ref: kvErr.Ref,
RetryReasons: translateCoreRetryReasons(kvErr.RetryReasons),
RetryAttempts: kvErr.RetryAttempts,
LastDispatchedTo: kvErr.LastDispatchedTo,
LastDispatchedFrom: kvErr.LastDispatchedFrom,
LastConnectionID: kvErr.LastConnectionID,
}
}
if viewErr, ok := err.(*gocbcore.ViewError); ok {
return &ViewError{
InnerError: viewErr.InnerError,
DesignDocumentName: viewErr.DesignDocumentName,
ViewName: viewErr.ViewName,
Errors: translateCoreViewErrorDesc(viewErr.Errors),
Endpoint: viewErr.Endpoint,
RetryReasons: translateCoreRetryReasons(viewErr.RetryReasons),
RetryAttempts: viewErr.RetryAttempts,
ErrorText: viewErr.ErrorText,
HTTPStatusCode: viewErr.HTTPResponseCode,
}
}
if queryErr, ok := err.(*gocbcore.N1QLError); ok {
inner := queryErr.InnerError
if errors.Is(inner, ErrFeatureNotAvailable) {
if len(queryErr.Errors) > 0 {
desc := queryErr.Errors[0]
// We replace the gocbcore wrapped inner feature not available error with our own to provide gocb
// specific context for the user.
if desc.Code == 1197 {
inner = wrapError(ErrFeatureNotAvailable, "this server requires that scope.Query() is used rather than "+
"cluster.Query(), if this is a transaction then pass a Scope within TransactionQueryOptions")
}
}
}
return &QueryError{
InnerError: inner,
Statement: queryErr.Statement,
ClientContextID: queryErr.ClientContextID,
Errors: translateCoreQueryErrorDesc(queryErr.Errors),
Endpoint: queryErr.Endpoint,
RetryReasons: translateCoreRetryReasons(queryErr.RetryReasons),
RetryAttempts: queryErr.RetryAttempts,
ErrorText: queryErr.ErrorText,
HTTPStatusCode: queryErr.HTTPResponseCode,
}
}
if analyticsErr, ok := err.(*gocbcore.AnalyticsError); ok {
return &AnalyticsError{
InnerError: analyticsErr.InnerError,
Statement: analyticsErr.Statement,
ClientContextID: analyticsErr.ClientContextID,
Errors: translateCoreAnalyticsErrorDesc(analyticsErr.Errors),
Endpoint: analyticsErr.Endpoint,
RetryReasons: translateCoreRetryReasons(analyticsErr.RetryReasons),
RetryAttempts: analyticsErr.RetryAttempts,
ErrorText: analyticsErr.ErrorText,
HTTPStatusCode: analyticsErr.HTTPResponseCode,
}
}
if searchErr, ok := err.(*gocbcore.SearchError); ok {
return &SearchError{
InnerError: searchErr.InnerError,
Query: searchErr.Query,
Endpoint: searchErr.Endpoint,
RetryReasons: translateCoreRetryReasons(searchErr.RetryReasons),
RetryAttempts: searchErr.RetryAttempts,
ErrorText: searchErr.ErrorText,
IndexName: searchErr.IndexName,
HTTPStatusCode: searchErr.HTTPResponseCode,
}
}
if httpErr, ok := err.(*gocbcore.HTTPError); ok {
return &HTTPError{
InnerError: httpErr.InnerError,
UniqueID: httpErr.UniqueID,
Endpoint: httpErr.Endpoint,
RetryReasons: translateCoreRetryReasons(httpErr.RetryReasons),
RetryAttempts: httpErr.RetryAttempts,
}
}
if timeoutErr, ok := err.(*gocbcore.TimeoutError); ok {
return &TimeoutError{
InnerError: timeoutErr.InnerError,
OperationID: timeoutErr.OperationID,
Opaque: timeoutErr.Opaque,
TimeObserved: timeoutErr.TimeObserved,
RetryReasons: translateCoreRetryReasons(timeoutErr.RetryReasons),
RetryAttempts: timeoutErr.RetryAttempts,
LastDispatchedTo: timeoutErr.LastDispatchedTo,
LastDispatchedFrom: timeoutErr.LastDispatchedFrom,
LastConnectionID: timeoutErr.LastConnectionID,
}
}
return err
}
func maybeEnhanceKVErr(err error, bucketName, scopeName, collName, docKey string) error {
return maybeEnhanceCoreErr(err)
}
func maybeEnhanceCollKVErr(err error, bucket kvProvider, coll *Collection, docKey string) error {
return maybeEnhanceKVErr(err, coll.bucketName(), coll.Name(), coll.ScopeName(), docKey)
}
func maybeEnhanceViewError(err error) error {
return maybeEnhanceCoreErr(err)
}
func maybeEnhanceQueryError(err error) error {
return maybeEnhanceCoreErr(err)
}
func maybeEnhanceAnalyticsError(err error) error {
return maybeEnhanceCoreErr(err)
}
func maybeEnhanceSearchError(err error) error {
return maybeEnhanceCoreErr(err)
}