-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
116 lines (92 loc) · 3.74 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
package authenticator
import (
"errors"
"fmt"
)
const (
// EInvalidToken represents an invalid JWT token error.
EInvalidToken ErrCode = "invalid_token"
// EInvalidCode represents an invalid OTP code.
EInvalidCode ErrCode = "invalid_code"
// EInvalidField represents an entity field error in a repository.
EInvalidField ErrCode = "invalid_field"
// EInternal represents an internal error outside of our domain.
EInternal ErrCode = "internal"
// EBadRequest represents a bad JSON request body.
EBadRequest ErrCode = "bad_request"
// ENotFound represents a non existent entity.
ENotFound ErrCode = "not_found"
// EWebAuthn represents a webauthn authentication error.
EWebAuthn ErrCode = "webauthn"
// EThrottle represents a rate limiting error.
EThrottle ErrCode = "too_many_requests"
)
// Error represents an error within the authenticator domain.
type Error interface {
Error() string
Message() string
Code() ErrCode
}
// ErrCode is a machine readable code representing
// an error within the authenticator domain.
type ErrCode string
// ErrInvalidCode represents an error related to an invalid TOTP/OTP code.
type ErrInvalidCode string
func (e ErrInvalidCode) Code() ErrCode { return EInvalidCode }
func (e ErrInvalidCode) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrInvalidCode) Message() string { return string(e) }
// ErrInvalidToken represents an error related to JWT token invalidation
// such as expiry, revocation, or signing errors.
type ErrInvalidToken string
func (e ErrInvalidToken) Code() ErrCode { return EInvalidToken }
func (e ErrInvalidToken) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrInvalidToken) Message() string { return string(e) }
// ErrInvalidField represents an error related to missing or invalid entity fields.
type ErrInvalidField string
func (e ErrInvalidField) Code() ErrCode { return EInvalidField }
func (e ErrInvalidField) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrInvalidField) Message() string { return string(e) }
// ErrBadRequest represents an error where we fail to read a JSON requst body.
type ErrBadRequest string
func (e ErrBadRequest) Code() ErrCode { return EBadRequest }
func (e ErrBadRequest) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrBadRequest) Message() string { return string(e) }
// ErrNotFound represents an error where we fail to read a JSON requst body.
type ErrNotFound string
func (e ErrNotFound) Code() ErrCode { return ENotFound }
func (e ErrNotFound) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrNotFound) Message() string { return string(e) }
// ErrWebAuthn represents an error related to webauthn authentication.
type ErrWebAuthn string
func (e ErrWebAuthn) Code() ErrCode { return EWebAuthn }
func (e ErrWebAuthn) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrWebAuthn) Message() string { return string(e) }
// ErrThrottle represents a rate limiting error.
type ErrThrottle string
func (e ErrThrottle) Code() ErrCode { return EThrottle }
func (e ErrThrottle) Error() string { return fmt.Sprintf("[%s] %s", e.Code(), string(e)) }
func (e ErrThrottle) Message() string { return string(e) }
// DomainError returns a domain error if available.
func DomainError(err error) Error {
if err == nil {
return nil
}
var e Error
if errors.As(err, &e) {
return e
}
return nil
}
// ErrorCode returns the code associated with a domain error.
// If an error is not part of the authenticator domain, it
// returns Internal.
func ErrorCode(err error) ErrCode {
if err == nil {
return ErrCode("")
}
e := DomainError(err)
if e == nil {
return EInternal
}
return e.Code()
}