-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcommon.go
109 lines (93 loc) · 2.8 KB
/
common.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
//nolint
package errors
import (
"fmt"
"reflect"
abci "github.com/tendermint/abci/types"
)
var (
errDecoding = fmt.Errorf("Error decoding input")
errUnauthorized = fmt.Errorf("Unauthorized")
errTooLarge = fmt.Errorf("Input size too large")
errMissingSignature = fmt.Errorf("Signature missing")
errUnknownTxType = fmt.Errorf("Tx type unknown")
errInvalidFormat = fmt.Errorf("Invalid format")
errUnknownModule = fmt.Errorf("Unknown module")
errUnknownKey = fmt.Errorf("Unknown key")
internalErr = abci.CodeType_InternalError
encodingErr = abci.CodeType_EncodingError
unauthorized = abci.CodeType_Unauthorized
unknownRequest = abci.CodeType_UnknownRequest
unknownAddress = abci.CodeType_BaseUnknownAddress
)
// some crazy reflection to unwrap any generated struct.
func unwrap(i interface{}) interface{} {
v := reflect.ValueOf(i)
m := v.MethodByName("Unwrap")
if m.IsValid() {
out := m.Call(nil)
if len(out) == 1 {
return out[0].Interface()
}
}
return i
}
func ErrUnknownTxType(tx interface{}) TMError {
msg := fmt.Sprintf("%T", unwrap(tx))
return WithMessage(msg, errUnknownTxType, unknownRequest)
}
func IsUnknownTxTypeErr(err error) bool {
return IsSameError(errUnknownTxType, err)
}
func ErrInvalidFormat(expected string, tx interface{}) TMError {
msg := fmt.Sprintf("%T not %s", unwrap(tx), expected)
return WithMessage(msg, errInvalidFormat, unknownRequest)
}
func IsInvalidFormatErr(err error) bool {
return IsSameError(errInvalidFormat, err)
}
func ErrUnknownModule(mod string) TMError {
return WithMessage(mod, errUnknownModule, unknownRequest)
}
func IsUnknownModuleErr(err error) bool {
return IsSameError(errUnknownModule, err)
}
func ErrUnknownKey(mod string) TMError {
return WithMessage(mod, errUnknownKey, unknownRequest)
}
func IsUnknownKeyErr(err error) bool {
return IsSameError(errUnknownKey, err)
}
func ErrInternal(msg string) TMError {
return New(msg, internalErr)
}
// IsInternalErr matches any error that is not classified
func IsInternalErr(err error) bool {
return HasErrorCode(err, internalErr)
}
func ErrDecoding() TMError {
return WithCode(errDecoding, encodingErr)
}
func IsDecodingErr(err error) bool {
return IsSameError(errDecoding, err)
}
func ErrUnauthorized() TMError {
return WithCode(errUnauthorized, unauthorized)
}
// IsUnauthorizedErr is generic helper for any unauthorized errors,
// also specific sub-types
func IsUnauthorizedErr(err error) bool {
return HasErrorCode(err, unauthorized)
}
func ErrMissingSignature() TMError {
return WithCode(errMissingSignature, unauthorized)
}
func IsMissingSignatureErr(err error) bool {
return IsSameError(errMissingSignature, err)
}
func ErrTooLarge() TMError {
return WithCode(errTooLarge, encodingErr)
}
func IsTooLargeErr(err error) bool {
return IsSameError(errTooLarge, err)
}