-
Notifications
You must be signed in to change notification settings - Fork 182
/
errors.go
91 lines (76 loc) · 3.2 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
package common
import (
"encoding/json"
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// const uint32
const (
DefaultCodespace = "common"
CodeInternalError uint32 = 60100
CodeInvalidPaginateParam uint32 = 60101
CodeCreateAddrFromBech32Failed uint32 = 60102
CodeMarshalJSONFailed uint32 = 60103
CodeUnMarshalJSONFailed uint32 = 60104 //"incorrectly formatted request data", err.Error()
CodeStrconvFailed uint32 = 60105
CodeUnknownProposalType uint32 = 60106
CodeInsufficientCoins uint32 = 60107
CodeMissingRequiredParam uint32 = 60108
CodeInvalidParam uint32 = 60109
CodeServerException uint32 = 60110
CodeDataNotExist uint32 = 60111
CodeCodecFails uint32 = 60112
CodeABCIQueryFails uint32 = 60113
CodeArgsWithLimit uint32 = 60114
)
type SDKError struct {
Codespace string `json:"codespace"`
Code uint32 `json:"code"`
Message string `json:"message"`
}
func ParseSDKError(errMsg string) SDKError {
var res abci.ResponseQuery
err := json.Unmarshal([]byte(errMsg), &res)
if err != nil {
return SDKError{
Codespace: DefaultCodespace,
Code: CodeInternalError,
Message: "query response unmarshal failed" + err.Error(),
}
}
return SDKError{
Codespace: res.Codespace,
Code: res.Code,
Message: res.Log,
}
}
func ErrInvalidParam(msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(DefaultCodespace, CodeInvalidParam, msg)}
}
// invalid paginate param
func ErrInvalidPaginateParam(page int, perPage int) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(DefaultCodespace, CodeInvalidPaginateParam, fmt.Sprintf("negative page %d or per_page %d is invalid", page, perPage))}
}
// invalid address
func ErrCreateAddrFromBech32Failed(addr string, msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(DefaultCodespace, CodeCreateAddrFromBech32Failed, fmt.Sprintf("invalid address:%s, reason: %s", addr, msg))}
}
// could not marshal result to JSON
func ErrMarshalJSONFailed(msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(DefaultCodespace, CodeMarshalJSONFailed, fmt.Sprintf("could not marshal result to JSON, %s", msg))}
}
// could not unmarshal result to origin
func ErrUnMarshalJSONFailed(msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(DefaultCodespace, CodeUnMarshalJSONFailed, fmt.Sprintf("incorrectly formatted request data, %s", msg))}
}
func ErrStrconvFailed(msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(DefaultCodespace, CodeStrconvFailed, fmt.Sprintf("incorrect string conversion"))}
}
func ErrUnknownProposalType(codespace string, msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{Err: sdkerrors.New(codespace, CodeUnknownProposalType, fmt.Sprintf("unknown proposal content type: %s", msg))}
}
func ErrInsufficientCoins(codespace string, msg string) sdk.EnvelopedErr {
return sdk.EnvelopedErr{sdkerrors.New(codespace, CodeInsufficientCoins, fmt.Sprintf("insufficient coins: %s", msg))}
}