-
Notifications
You must be signed in to change notification settings - Fork 13
/
error.go
53 lines (45 loc) · 1.05 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
package mixin
import (
"errors"
"fmt"
)
// mixin error codes https://developers.mixin.one/api/alpha-mixin-network/errors/
const (
Unauthorized = 401
EndpointNotFound = 404
InsufficientBalance = 20117
PinIncorrect = 20119
InsufficientFee = 20124
InvalidTraceID = 20125
)
type Error struct {
Status int `json:"status"`
Code int `json:"code"`
Description string `json:"description"`
Extra map[string]interface{} `json:"extra,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)
}
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,
}
}