-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherrors.go
61 lines (51 loc) · 1.48 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
package redtape
import (
"net/http"
"github.com/pkg/errors"
)
// Error is a customized error implementation with additional context for policy evaluation.
type Error struct {
code int
id string
reason string
status string
error
}
// StatusCode can contain application or standard integer codes eg http 401.
func (e *Error) StatusCode() int {
return e.code
}
// RequestID allows errors to be tracked against custom request ids.
func (e *Error) RequestID() string {
return e.id
}
// Status is a text explanation of the error code.
func (e *Error) Status() string {
return e.status
}
// Reason contains information about the policy decision that resulted in the error.
func (e *Error) Reason() string {
return e.reason
}
// NewErrRequestDeniedExplicit returns an error with for explicit denials.
func NewErrRequestDeniedExplicit(p Policy) error {
return errors.WithStack(&Error{
error: errors.New("access denied"),
id: p.ID(),
code: http.StatusForbidden,
status: http.StatusText(http.StatusForbidden),
reason: "request denied because a policy explicitly forbids it",
})
}
// NewErrRequestDeniedImplicit returns an error with for implicit denials (no policy).
func NewErrRequestDeniedImplicit(err error) error {
if err == nil {
err = errors.New("request denied")
}
return errors.WithStack(&Error{
error: err,
code: http.StatusForbidden,
status: http.StatusText(http.StatusForbidden),
reason: "request denied because no matching policy was found",
})
}