-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
138 lines (113 loc) · 3.04 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package rbac
import (
"net/http"
"github.com/pkg/errors"
)
// AuthErrorInterface represents an error with an associated, suggested HTTP status code and internal error code.
// Not directly used by this package but is exported to aid consumer packages testing against errors from this package.
type AuthErrorInterface interface {
Code() int32
Error() string
StatusCode() int32
StackTrace() errors.StackTrace
}
// authError represents an error with an associated HTTP status code.
// Implements default error interface.
type authError struct {
err error
code int32
status int32
}
// Error allows Error to satisfy the default error interface.
func (e authError) Error() string {
return e.err.Error()
}
// StackTrace returns the error stacktrace and satisfies the errors.Stacktrace interface.
func (e authError) StackTrace() errors.StackTrace {
type stackTracer interface {
StackTrace() errors.StackTrace
}
if err, ok := e.err.(stackTracer); ok && err != nil {
return err.StackTrace()
}
return nil
}
func (e authError) Code() int32 {
return e.code
}
// StatusCode returns the suggest http status code.
func (e authError) StatusCode() int32 {
return e.status
}
// Error Codes for identifying error types.
const (
CodeErrQueryMarshal = 4001
CodeErrQueryDocType = 4002
CodeErrAuthentication = 4011
CodeErrRoles = 4031
CodeErrContract = 4032
CodeErrQuery = 4033
)
// errAuthentication for authentication errors (user could not be authenticated).
func errAuthentication(err error) authError {
err = errors.Wrap(err, "user authentication failed")
return authError{
err: err,
code: CodeErrAuthentication,
status: http.StatusUnauthorized,
}
}
// errRoles error.
func errRoles(role string) authError {
err := errors.Errorf("user roles not found. `%v` attribute does not exist on identity", role)
return authError{
err: err,
code: CodeErrRoles,
status: http.StatusForbidden,
}
}
// errContract error.
func errContract() authError {
err := errors.New("user doesn't have permission to invoke this contract")
return authError{
err: err,
code: CodeErrContract,
status: http.StatusForbidden,
}
}
// errQuery error.
func errQuery(res string) authError {
err := errors.Errorf("user doesn't have permission to query %v records", res)
return authError{
err: err,
code: CodeErrQuery,
status: http.StatusForbidden,
}
}
// errQueryMarshal error.
func errQueryMarshal(err error) authError {
err = errors.Wrap(err, "could not marshal query")
return authError{
err: err,
code: CodeErrQueryMarshal,
status: http.StatusBadRequest,
}
}
// errQueryDocType error.
func errQueryDocType() authError {
err := errors.New("docType not found in query, did you include it at the root?")
return authError{
err: err,
code: CodeErrQueryDocType,
status: http.StatusBadRequest,
}
}
// errMarshal error.
func errMarshal(err error) authError {
err = errors.Wrap(err, "Marshal failed")
return authError{
err: err,
code: CodeErrQueryDocType,
status: http.StatusBadRequest,
}
}