forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
124 lines (103 loc) · 3.8 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
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
package errors
import (
"fmt"
)
// UndefinedCodespace when we explicitly declare no codespace
const UndefinedCodespace = "undefined"
var (
// ErrStopIterating is used to break out of an iteration
ErrStopIterating = Register(UndefinedCodespace, 2, "stop iterating")
// ErrPanic should only be set when we recovering from a panic
ErrPanic = Register(UndefinedCodespace, 111222, "panic")
)
// Register returns an error instance that should be used as the base for
// creating error instances during runtime.
//
// Popular root errors are declared in this package, but extensions may want to
// declare custom codes. This function ensures that no error code is used
// twice. Attempt to reuse an error code results in panic.
//
// Use this function only during a program startup phase.
func Register(codespace string, code uint32, description string) *Error {
if e := getUsed(codespace, code); e != nil {
panic(fmt.Sprintf("error with code %d is already registered: %q", code, e.desc))
}
err := &Error{codespace: codespace, code: code, desc: description}
setUsed(err)
return err
}
// usedCodes is keeping track of used codes to ensure their uniqueness. No two
// error instances should share the same (codespace, code) tuple.
var usedCodes = map[string]*Error{}
func errorID(codespace string, code uint32) string {
return fmt.Sprintf("%s:%d", codespace, code)
}
func getUsed(codespace string, code uint32) *Error {
return usedCodes[errorID(codespace, code)]
}
func setUsed(err *Error) {
usedCodes[errorID(err.codespace, err.code)] = err
}
// ABCIError will resolve an error code/log from an abci result into
// an error message. If the code is registered, it will map it back to
// the canonical error, so we can do eg. ErrNotFound.Is(err) on something
// we get back from an external API.
//
// This should *only* be used in clients, not in the server side.
// The server (abci app / blockchain) should only refer to registered errors
func ABCIError(codespace string, code uint32, log string) error {
if e := getUsed(codespace, code); e != nil {
return fmt.Errorf("%s: %w", log, e)
}
// This is a unique error, will never match on .Is()
// Use Wrap here to get a stack trace
return fmt.Errorf("%s: %w", log, &Error{codespace: codespace, code: code, desc: "unknown"})
}
// Error represents a root error.
//
// Weave framework is using root error to categorize issues. Each instance
// created during the runtime should wrap one of the declared root errors. This
// allows error tests and returning all errors to the client in a safe manner.
//
// All popular root errors are declared in this package. If an extension has to
// declare a custom root error, always use Register function to ensure
// error code uniqueness.
type Error struct {
codespace string
code uint32
desc string
}
// New is an alias for Register.
func New(codespace string, code uint32, desc string) *Error {
return Register(codespace, code, desc)
}
func (e Error) Error() string {
return e.desc
}
func (e Error) ABCICode() uint32 {
return e.code
}
func (e Error) Codespace() string {
return e.codespace
}
// Wrap extends given error with an additional information.
//
// If the wrapped error does not provide ABCICode method (ie. stdlib errors),
// it will be labeled as internal error.
//
// If err is nil, this returns nil, avoiding the need for an if statement when
// wrapping a error returned at the end of a function
func Wrap(err error, description string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", description, err)
}
// Wrapf extends given error with an additional information.
//
// This function works like Wrap function with additional functionality of
// formatting the input as specified.
func Wrapf(err error, format string, args ...interface{}) error {
desc := fmt.Sprintf(format, args...)
return Wrap(err, desc)
}