-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
115 lines (101 loc) · 2.61 KB
/
errors.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
package arangolite
import (
"fmt"
)
func withMessage(err error, message string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %s", message, err.Error())
}
type arangoError struct {
error
statusCode int
errorNum int
}
func withStatusCode(err error, statusCode int) error {
aError, ok := err.(arangoError)
if ok {
aError.statusCode = statusCode
} else {
aError = arangoError{error: err, statusCode: statusCode}
}
return aError
}
func withErrorNum(err error, errorNum int) error {
aError, ok := err.(arangoError)
if ok {
aError.errorNum = errorNum
} else {
aError = arangoError{error: err, errorNum: errorNum}
}
return aError
}
// HasStatusCode returns true when one of the given error status code matches the one returned by the database.
func HasStatusCode(err error, statusCode ...int) bool {
e, ok := err.(arangoError)
if !ok {
return false
}
code := e.statusCode
for _, c := range statusCode {
if code == c {
return true
}
break
}
return false
}
// GetStatusCode returns the status code encapsulated in the error.
func GetStatusCode(err error) (code int, ok bool) {
e, ok := err.(arangoError)
if !ok || e.statusCode == 0 {
return 0, false
}
return e.statusCode, true
}
// HasErrorNum returns true when one of the given error num matches the one returned by the database.
func HasErrorNum(err error, errorNum ...int) bool {
e, ok := err.(arangoError)
if !ok {
return false
}
num := e.errorNum
for _, n := range errorNum {
if num == n {
return true
}
break
}
return false
}
// GetErrorNum returns the database error num encapsulated in the error.
func GetErrorNum(err error) (errorNum int, ok bool) {
e, ok := err.(arangoError)
if !ok || e.errorNum == 0 {
return 0, false
}
return e.errorNum, true
}
// IsErrInvalidRequest returns true when the database returns a 400.
func IsErrInvalidRequest(err error) bool {
return HasStatusCode(err, 400)
}
// IsErrUnauthorized returns true when the database returns a 401.
func IsErrUnauthorized(err error) bool {
return HasStatusCode(err, 401)
}
// IsErrForbidden returns true when the database returns a 403.
func IsErrForbidden(err error) bool {
return HasStatusCode(err, 403)
}
// IsErrUnique returns true when the error num is a 1210 - ERROR_ARANGO_UNIQUE_CONSTRAINT_VIOLATED.
func IsErrUnique(err error) bool {
return HasErrorNum(err, 1210)
}
// IsErrNotFound returns true when the database returns a 404 or when the error num is:
// 1202 - ERROR_ARANGO_DOCUMENT_NOT_FOUND
// 1203 - ERROR_ARANGO_COLLECTION_NOT_FOUND
func IsErrNotFound(err error) bool {
return HasStatusCode(err, 404) || HasErrorNum(err, 1202, 1203)
}