/
error.go
73 lines (60 loc) · 1.58 KB
/
error.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
package mixinnet
import (
"errors"
"fmt"
"strings"
)
// mixin error codes https://developers.mixin.one/api/alpha-mixin-network/errors/
const (
InvalidOutputKey = 2000001
InputLocked = 2000002
InvalidSignature = 2000003
)
type Error struct {
Status int `json:"status"`
Code int `json:"code"`
Description string `json:"description"`
Extra map[string]interface{} `json:"extra,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
func (e *Error) Error() string {
s := fmt.Sprintf("[%d/%d] %s", e.Status, e.Code, e.Description)
for k, v := range e.Extra {
s += fmt.Sprintf(" %v=%v", k, v)
}
if e.RequestID != "" {
s += fmt.Sprintf(" id=%s", e.RequestID)
}
return s
}
func IsErrorCodes(err error, codes ...int) bool {
var e *Error
if errors.As(err, &e) {
for _, code := range codes {
if e.Code == code {
return true
}
}
}
return false
}
func createError(status, code int, description string) error {
return &Error{
Status: status,
Code: code,
Description: description,
}
}
func parseError(errMsg string) error {
if strings.HasPrefix(errMsg, "invalid output key ") {
return createError(202, InvalidOutputKey, errMsg)
}
if strings.HasPrefix(errMsg, "input locked for transaction ") {
return createError(202, InputLocked, errMsg)
}
if strings.HasPrefix(errMsg, "invalid tx signature number ") ||
strings.HasPrefix(errMsg, "invalid signature keys ") {
return createError(202, InvalidSignature, errMsg)
}
return createError(202, 202, errMsg)
}