/
errors_exchain.go
201 lines (181 loc) · 4.67 KB
/
errors_exchain.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package types
import (
"fmt"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/types/errors"
"strings"
)
type Error = error
type EnvelopedErr struct {
Err Error
}
type Coder interface {
ABCICode() uint32
}
type Causer interface {
Cause() error
}
type Codespacer interface {
Codespace() string
}
func (e EnvelopedErr) ABCICode() uint32 {
err := e.Err
if err == nil {
return errors.SuccessABCICode
}
for {
if c, ok := err.(Coder); ok {
return c.ABCICode()
}
if c, ok := err.(Causer); ok {
err = c.Cause()
} else {
return 1
}
}
}
func (e EnvelopedErr) Codespace() string {
err := e.Err
if err == nil {
return ""
}
for {
if c, ok := err.(Codespacer); ok {
return c.Codespace()
}
if c, ok := err.(Causer); ok {
err = c.Cause()
} else {
return errors.UndefinedCodespace
}
}
}
const (
CodeOK uint32 = 0
CodeInternal uint32 = 23
CodeGasOverflow uint32 = 22
)
var (
// Base error codes
CodeTxDecode uint32 = errors.ErrTxDecode.ABCICode()
CodeInvalidSequence uint32 = errors.ErrInvalidSequence.ABCICode()
CodeUnauthorized uint32 = errors.ErrUnauthorized.ABCICode()
CodeInsufficientFunds uint32 = errors.ErrInsufficientFunds.ABCICode()
CodeUnknownRequest uint32 = errors.ErrUnknownRequest.ABCICode()
CodeInvalidAddress uint32 = errors.ErrInvalidAddress.ABCICode()
CodeInvalidPubKey uint32 = errors.ErrInvalidPubKey.ABCICode()
CodeUnknownAddress uint32 = errors.ErrUnknownAddress.ABCICode()
CodeInsufficientCoins uint32 = errors.ErrInsufficientFunds.ABCICode()
CodeInvalidCoins uint32 = errors.ErrInvalidCoins.ABCICode()
CodeOutOfGas uint32 = errors.ErrOutOfGas.ABCICode()
CodeMemoTooLarge uint32 = errors.ErrMemoTooLarge.ABCICode()
CodeInsufficientFee uint32 = errors.ErrInsufficientFee.ABCICode()
CodeTooManySignatures uint32 = errors.ErrTooManySignatures.ABCICode()
CodeNoSignatures uint32 = errors.ErrNoSignatures.ABCICode()
)
var errInternal = errors.Register(errors.UndefinedCodespace, CodeInternal, "internal")
var errGasOverflow = errors.Register(errors.RootCodespace, CodeGasOverflow, "gas overflow")
func (err EnvelopedErr) Result() (*Result, error) {
return nil, err.Err
}
func (err EnvelopedErr) Error() string {
return err.Err.Error()
}
// appends a message to the head of the given error
func AppendMsgToErr(msg string, err string) string {
msgIdx := strings.Index(err, "message\":\"")
if msgIdx != -1 {
errMsg := err[msgIdx+len("message\":\"") : len(err)-2]
errMsg = fmt.Sprintf("%s; %s", msg, errMsg)
return fmt.Sprintf("%s%s%s",
err[:msgIdx+len("message\":\"")],
errMsg,
err[len(err)-2:],
)
}
return fmt.Sprintf("%s; %s", msg, err)
}
func ErrInternal(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errInternal, msg),
}
}
func ErrTxDecode(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrTxDecode, msg),
}
}
func ErrInvalidSequence(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInvalidSequence, msg),
}
}
func ErrUnauthorized(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrUnauthorized, msg),
}
}
func ErrInsufficientFunds(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInsufficientFunds, msg),
}
}
func ErrUnknownRequest(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrUnknownRequest, msg),
}
}
func ErrInvalidAddress(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInvalidAddress, msg),
}
}
func ErrUnknownAddress(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrUnknownAddress, msg),
}
}
func ErrInvalidPubKey(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInvalidPubKey, msg),
}
}
func ErrInsufficientCoins(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInsufficientFunds, msg),
}
}
func ErrInvalidCoins(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInvalidCoins, msg),
}
}
func ErrOutOfGas(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrOutOfGas, msg),
}
}
func ErrMemoTooLarge(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrMemoTooLarge, msg),
}
}
func ErrInsufficientFee(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrInsufficientFee, msg),
}
}
func ErrTooManySignatures(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrTooManySignatures, msg),
}
}
func ErrNoSignatures(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errors.ErrNoSignatures, msg),
}
}
func ErrGasOverflow(msg string) EnvelopedErr {
return EnvelopedErr{
errors.Wrapf(errGasOverflow, msg),
}
}