-
Notifications
You must be signed in to change notification settings - Fork 364
/
errors.go
81 lines (68 loc) · 1.46 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
package keyerror
import (
"fmt"
)
const (
codeKeyNotFound = 1
codeWrongPassword = 2
)
type keybaseError interface {
error
Code() int
}
type keyNotFoundError struct {
code int
name string
}
func (e keyNotFoundError) Code() int {
return e.code
}
func (e keyNotFoundError) Error() string {
return fmt.Sprintf("Key %s not found", e.name)
}
// NewErrKeyNotFound returns a standardized error reflecting that the specified key doesn't exist
func NewErrKeyNotFound(name string) error {
return keyNotFoundError{
code: codeKeyNotFound,
name: name,
}
}
// IsErrKeyNotFound returns true if the given error is keyNotFoundError
func IsErrKeyNotFound(err error) bool {
if err == nil {
return false
}
if keyErr, ok := err.(keybaseError); ok {
if keyErr.Code() == codeKeyNotFound {
return true
}
}
return false
}
type wrongPasswordError struct {
code int
}
func (e wrongPasswordError) Code() int {
return e.code
}
func (e wrongPasswordError) Error() string {
return "invalid account password"
}
// NewErrWrongPassword returns a standardized error reflecting that the specified password is wrong
func NewErrWrongPassword() error {
return wrongPasswordError{
code: codeWrongPassword,
}
}
// IsErrWrongPassword returns true if the given error is wrongPasswordError
func IsErrWrongPassword(err error) bool {
if err == nil {
return false
}
if keyErr, ok := err.(keybaseError); ok {
if keyErr.Code() == codeWrongPassword {
return true
}
}
return false
}