forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_test.go
137 lines (122 loc) · 3.42 KB
/
errors_test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package types
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var codeTypes = []CodeType{
CodeInternal,
CodeTxDecode,
CodeInvalidSequence,
CodeUnauthorized,
CodeInsufficientFunds,
CodeUnknownRequest,
CodeInvalidAddress,
CodeInvalidPubKey,
CodeUnknownAddress,
CodeInsufficientCoins,
CodeInvalidCoins,
CodeOutOfGas,
CodeMemoTooLarge,
}
type errFn func(msg string) Error
var errFns = []errFn{
ErrInternal,
ErrTxDecode,
ErrInvalidSequence,
ErrUnauthorized,
ErrInsufficientFunds,
ErrUnknownRequest,
ErrInvalidAddress,
ErrInvalidPubKey,
ErrUnknownAddress,
ErrInsufficientCoins,
ErrInvalidCoins,
ErrOutOfGas,
ErrMemoTooLarge,
}
func TestCodeType(t *testing.T) {
require.True(t, CodeOK.IsOK())
for tcnum, c := range codeTypes {
msg := CodeToDefaultMsg(c)
require.NotEqual(t, unknownCodeMsg(c), msg, "Code expected to be known. tc #%d, code %d, msg %s", tcnum, c, msg)
}
msg := CodeToDefaultMsg(CodeOK)
require.Equal(t, unknownCodeMsg(CodeOK), msg)
}
func TestErrFn(t *testing.T) {
for i, errFn := range errFns {
err := errFn("")
codeType := codeTypes[i]
require.Equal(t, err.Code(), codeType, "Err function expected to return proper code. tc #%d", i)
require.Equal(t, err.Codespace(), CodespaceRoot, "Err function expected to return proper codespace. tc #%d", i)
require.Equal(t, err.QueryResult().Code, uint32(err.Code()), "Err function expected to return proper Code from QueryResult. tc #%d")
require.Equal(t, err.QueryResult().Log, err.ABCILog(), "Err function expected to return proper ABCILog from QueryResult. tc #%d")
}
}
func TestAppendMsgToErr(t *testing.T) {
for i, errFn := range errFns {
err := errFn("")
errMsg := err.Stacktrace().Error()
abciLog := err.ABCILog()
// plain msg error
msg := AppendMsgToErr("something unexpected happened", errMsg)
require.Equal(
t,
fmt.Sprintf("something unexpected happened; %s", errMsg),
msg,
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i),
)
// ABCI Log msg error
msg = AppendMsgToErr("something unexpected happened", abciLog)
msgIdx := mustGetMsgIndex(abciLog)
require.Equal(
t,
fmt.Sprintf("%s%s; %s}",
abciLog[:msgIdx],
"something unexpected happened",
abciLog[msgIdx:len(abciLog)-1],
),
msg,
fmt.Sprintf("Should have formatted the error message of ABCI Log. tc #%d", i))
}
}
func TestResultFromError(t *testing.T) {
cases := map[string]struct {
err error
expect Result
}{
"sdk.Error": {
err: ErrUnauthorized("not owner"),
expect: Result{
Codespace: CodespaceRoot,
Code: CodeUnauthorized,
Log: `{"codespace":"sdk","code":4,"message":"not owner"}`,
},
},
"types/errors": {
err: sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "not owner"),
expect: Result{
Codespace: CodespaceRoot,
Code: CodeUnauthorized,
Log: `{"codespace":"sdk","code":4,"message":"not owner: unauthorized"}`,
},
},
"stdlib errors": {
err: fmt.Errorf("not owner"),
expect: Result{
Codespace: CodespaceType("undefined"),
Code: CodeInternal,
// note that we redact the internal errors in the new package to not leak eg. panics
Log: `{"codespace":"undefined","code":1,"message":"internal error"}`,
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
res := ResultFromError(tc.err)
require.Equal(t, tc.expect, res)
})
}
}