Skip to content

Commit fa238c2

Browse files
committed
Move custom errors to errors.go
1 parent 574b9d2 commit fa238c2

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

errors.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@ package passhash
22

33
import (
44
"errors"
5+
"fmt"
6+
"strings"
57
)
68

79
var (
810
// ErrPasswordUnchanged is used when a Credential.ChangePassword*() method is called with the same old and new
911
// password
1012
ErrPasswordUnchanged = errors.New("Password unchanged")
1113
)
14+
15+
// PasswordPolicyError satisfies the error interface and describes the reason for a PasswordPolicy check failure
16+
type PasswordPolicyError struct {
17+
PasswordPolicy PasswordPolicy
18+
Err error
19+
}
20+
21+
func (e PasswordPolicyError) Error() string {
22+
return e.Err.Error()
23+
}
24+
25+
// PasswordPoliciesNotMet satisfies the error interface and tracks the unmet password policies
26+
type PasswordPoliciesNotMet struct {
27+
UnMetPasswordPolicies []PasswordPolicyError
28+
}
29+
30+
func (e PasswordPoliciesNotMet) Error() string {
31+
errorStrs := make([]string, 0, len(e.UnMetPasswordPolicies))
32+
for _, ppe := range e.UnMetPasswordPolicies {
33+
errorStrs = append(errorStrs, ppe.Error())
34+
}
35+
return fmt.Sprintf("Password policies not met due to: %s", strings.Join(errorStrs, ", "))
36+
}

password_policies.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package passhash
33
import (
44
"errors"
55
"fmt"
6-
"strings"
76
"unicode/utf8"
87
)
98

@@ -12,29 +11,6 @@ type PasswordPolicy interface {
1211
PasswordAcceptable(string) error
1312
}
1413

15-
// PasswordPolicyError satisfies the error interface and describes the reason for a PasswordPolicy check failure
16-
type PasswordPolicyError struct {
17-
PasswordPolicy PasswordPolicy
18-
Err error
19-
}
20-
21-
func (e PasswordPolicyError) Error() string {
22-
return e.Err.Error()
23-
}
24-
25-
// PasswordPoliciesNotMet satisfies the error interface and tracks the unmet password policies
26-
type PasswordPoliciesNotMet struct {
27-
UnMetPasswordPolicies []PasswordPolicyError
28-
}
29-
30-
func (e PasswordPoliciesNotMet) Error() string {
31-
errorStrs := make([]string, 0, len(e.UnMetPasswordPolicies))
32-
for _, ppe := range e.UnMetPasswordPolicies {
33-
errorStrs = append(errorStrs, ppe.Error())
34-
}
35-
return fmt.Sprintf("Password policies not met due to: %s", strings.Join(errorStrs, ", "))
36-
}
37-
3814
// AtLeastNRunes is a PasswordPolicy that ensures that the password is at least N runes in length
3915
type AtLeastNRunes struct {
4016
N int

0 commit comments

Comments
 (0)