forked from arangodb/go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
241 lines (208 loc) · 7.63 KB
/
error.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package driver
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"os"
)
// ArangoError is a Go error with arangodb specific error information.
type ArangoError struct {
HasError bool `json:"error"`
Code int `json:"code"`
ErrorNum int `json:"errorNum"`
ErrorMessage string `json:"errorMessage"`
}
// Error returns the error message of an ArangoError.
func (ae ArangoError) Error() string {
if ae.ErrorMessage != "" {
return ae.ErrorMessage
}
return fmt.Sprintf("ArangoError: Code %d, ErrorNum %d", ae.Code, ae.ErrorNum)
}
// Timeout returns true when the given error is a timeout error.
func (ae ArangoError) Timeout() bool {
return ae.HasError && (ae.Code == http.StatusRequestTimeout || ae.Code == http.StatusGatewayTimeout)
}
// Temporary returns true when the given error is a temporary error.
func (ae ArangoError) Temporary() bool {
return ae.HasError && ae.Code == http.StatusServiceUnavailable
}
// newArangoError creates a new ArangoError with given values.
func newArangoError(code, errorNum int, errorMessage string) error {
return ArangoError{
HasError: true,
Code: code,
ErrorNum: errorNum,
ErrorMessage: errorMessage,
}
}
// IsArangoError returns true when the given error is an ArangoError.
func IsArangoError(err error) bool {
ae, ok := Cause(err).(ArangoError)
return ok && ae.HasError
}
// IsArangoErrorWithCode returns true when the given error is an ArangoError and its Code field is equal to the given code.
func IsArangoErrorWithCode(err error, code int) bool {
ae, ok := Cause(err).(ArangoError)
return ok && ae.Code == code
}
// IsArangoErrorWithErrorNum returns true when the given error is an ArangoError and its ErrorNum field is equal to one of the given numbers.
func IsArangoErrorWithErrorNum(err error, errorNum ...int) bool {
ae, ok := Cause(err).(ArangoError)
if !ok {
return false
}
for _, x := range errorNum {
if ae.ErrorNum == x {
return true
}
}
return false
}
// IsInvalidRequest returns true if the given error is an ArangoError with code 400, indicating an invalid request.
func IsInvalidRequest(err error) bool {
return IsArangoErrorWithCode(err, 400)
}
// IsUnauthorized returns true if the given error is an ArangoError with code 401, indicating an unauthorized request.
func IsUnauthorized(err error) bool {
return IsArangoErrorWithCode(err, 401)
}
// IsForbidden returns true if the given error is an ArangoError with code 403, indicating a forbidden request.
func IsForbidden(err error) bool {
return IsArangoErrorWithCode(err, 403)
}
// IsNotFound returns true if the given error is an ArangoError with code 404, indicating a object not found.
func IsNotFound(err error) bool {
return IsArangoErrorWithCode(err, 404) || IsArangoErrorWithErrorNum(err, 1202, 1203)
}
// IsConflict returns true if the given error is an ArangoError with code 409, indicating a conflict.
func IsConflict(err error) bool {
return IsArangoErrorWithCode(err, 409) || IsArangoErrorWithErrorNum(err, 1702)
}
// IsPreconditionFailed returns true if the given error is an ArangoError with code 412, indicating a failed precondition.
func IsPreconditionFailed(err error) bool {
return IsArangoErrorWithCode(err, 412) || IsArangoErrorWithErrorNum(err, 1200, 1210)
}
// IsNoLeader returns true if the given error is an ArangoError with code 503 error number 1496.
func IsNoLeader(err error) bool {
return IsArangoErrorWithCode(err, 503) && IsArangoErrorWithErrorNum(err, 1496)
}
// IsNoLeaderOrOngoing return true if the given error is an ArangoError with code 503 and error number 1496 or 1495
func IsNoLeaderOrOngoing(err error) bool {
return IsArangoErrorWithCode(err, 503) && (IsArangoErrorWithErrorNum(err, 1495) || IsArangoErrorWithErrorNum(err, 1496))
}
// InvalidArgumentError is returned when a go function argument is invalid.
type InvalidArgumentError struct {
Message string
}
// Error implements the error interface for InvalidArgumentError.
func (e InvalidArgumentError) Error() string {
return e.Message
}
// IsInvalidArgument returns true if the given error is an InvalidArgumentError.
func IsInvalidArgument(err error) bool {
_, ok := Cause(err).(InvalidArgumentError)
return ok
}
// NoMoreDocumentsError is returned by Cursor's, when an attempt is made to read documents when there are no more.
type NoMoreDocumentsError struct{}
// Error implements the error interface for NoMoreDocumentsError.
func (e NoMoreDocumentsError) Error() string {
return "no more documents"
}
// IsNoMoreDocuments returns true if the given error is an NoMoreDocumentsError.
func IsNoMoreDocuments(err error) bool {
_, ok := Cause(err).(NoMoreDocumentsError)
return ok
}
// A ResponseError is returned when a request was completely written to a server, but
// the server did not respond, or some kind of network error occurred during the response.
type ResponseError struct {
Err error
}
// Error returns the Error() result of the underlying error.
func (e *ResponseError) Error() string {
return e.Err.Error()
}
// IsResponse returns true if the given error is (or is caused by) a ResponseError.
func IsResponse(err error) bool {
return isCausedBy(err, func(e error) bool { _, ok := e.(*ResponseError); return ok })
}
// IsCanceled returns true if the given error is the result on a cancelled context.
func IsCanceled(err error) bool {
return isCausedBy(err, func(e error) bool { return e == context.Canceled })
}
// IsTimeout returns true if the given error is the result on a deadline that has been exceeded.
func IsTimeout(err error) bool {
return isCausedBy(err, func(e error) bool { return e == context.DeadlineExceeded })
}
// isCausedBy returns true if the given error returns true on the given predicate,
// unwrapping various standard library error wrappers.
func isCausedBy(err error, p func(error) bool) bool {
if p(err) {
return true
}
err = Cause(err)
for {
if p(err) {
return true
} else if err == nil {
return false
}
if xerr, ok := err.(*ResponseError); ok {
err = xerr.Err
} else if xerr, ok := err.(*url.Error); ok {
err = xerr.Err
} else if xerr, ok := err.(*net.OpError); ok {
err = xerr.Err
} else if xerr, ok := err.(*os.SyscallError); ok {
err = xerr.Err
} else {
return false
}
}
}
var (
// WithStack is called on every return of an error to add stacktrace information to the error.
// When setting this function, also set the Cause function.
// The interface of this function is compatible with functions in github.com/pkg/errors.
WithStack = func(err error) error { return err }
// Cause is used to get the root cause of the given error.
// The interface of this function is compatible with functions in github.com/pkg/errors.
Cause = func(err error) error { return err }
)
// ErrorSlice is a slice of errors
type ErrorSlice []error
// FirstNonNil returns the first error in the slice that is not nil.
// If all errors in the slice are nil, nil is returned.
func (l ErrorSlice) FirstNonNil() error {
for _, e := range l {
if e != nil {
return e
}
}
return nil
}