-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
72 lines (61 loc) · 2.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
package p4ssw0rd
import (
"errors"
"fmt"
"github.com/dustin/go-humanize"
)
var (
// ErrMinLengthNotSatisfied indicates that a password does not meet the
// minimum length requirements
ErrMinLengthNotSatisfied = errors.New("minimum password length not satisfied")
// ErrBreachLimitExceeded indicates that the password's breach limit has
// been exceeded
ErrBreachLimitExceeded = errors.New("password breach limit exceeded")
// ErrMissingUserAgent is returned when a UserAgent is not specified
ErrMissingUserAgent = errors.New("UserAgent was not specified")
// ErrTooManyRequests occurs when have i been pwned returns a 429 this
// shouldn't happen per the docs: "There are 1,048,576 different hash
// prefixes between 00000 and FFFFF (16^5) and every single one will return
// HTTP 200; there is no circumstance in which the API should return HTTP
// 404."
ErrTooManyRequests = errors.New("error: too many requests — the rate limit has been exceeded")
// Service unavailable — usually returned by Cloudflare if the underlying
// service is not available
ErrServiceUnavailable = errors.New("error: service unavailable")
)
type baseErr struct {
Err error
}
func (e *baseErr) Unwrap() error {
return e.Err
}
func newBreachLimitError(count uint32) *BreachLimitError {
return &BreachLimitError{
baseErr: baseErr{Err: ErrBreachLimitExceeded},
BreachCount: count,
}
}
var _ error = (*BreachLimitError)(nil)
var _ error = (*MinLengthError)(nil)
type BreachLimitError struct {
baseErr
BreachCount uint32
}
func (e *BreachLimitError) Error() string {
return "breach count exceeded: found in " + humanize.Comma(int64(e.BreachCount)) + " data breaches"
}
type MinLengthError struct {
baseErr
MinRequired uint16
Length uint16
}
func newMinLengthError(required, length uint16) *MinLengthError {
return &MinLengthError{
baseErr: baseErr{Err: ErrMinLengthNotSatisfied},
MinRequired: required,
Length: length,
}
}
func (e *MinLengthError) Error() string {
return fmt.Sprintf("minimum length %d of not satisfied", e.MinRequired)
}