6 "github.com/bytom/bytom/account"
7 "github.com/bytom/bytom/asset"
8 "github.com/bytom/bytom/blockchain/pseudohsm"
9 "github.com/bytom/bytom/blockchain/rpc"
10 "github.com/bytom/bytom/blockchain/signers"
11 "github.com/bytom/bytom/blockchain/txbuilder"
12 "github.com/bytom/bytom/contract"
13 "github.com/bytom/bytom/errors"
14 "github.com/bytom/bytom/net/http/httperror"
15 "github.com/bytom/bytom/net/http/httpjson"
16 "github.com/bytom/bytom/protocol/validation"
17 "github.com/bytom/bytom/protocol/vm"
21 // ErrDefault is default Bytom API Error
22 ErrDefault = errors.New("Bytom API Error")
25 func isTemporary(info httperror.Info, err error) bool {
26 switch info.ChainCode {
27 case "BTM000": // internal server error
29 case "BTM001": // request timed out
31 case "BTM761": // outputs currently reserved
33 case "BTM706": // 1 or more action errors
34 errs := errors.Data(err)["actions"].([]httperror.Response)
36 for _, actionErr := range errs {
37 temp = temp && isTemporary(actionErr.Info, nil)
45 var respErrFormatter = map[error]httperror.Info{
46 ErrDefault: {500, "BTM000", "Bytom API Error"},
48 // Signers error namespace (2xx)
49 signers.ErrBadQuorum: {400, "BTM200", "Quorum must be greater than or equal to 1, and must be less than or equal to the length of xpubs"},
50 signers.ErrBadXPub: {400, "BTM201", "Invalid xpub format"},
51 signers.ErrNoXPubs: {400, "BTM202", "At least one xpub is required"},
52 signers.ErrDupeXPub: {400, "BTM203", "Root XPubs cannot contain the same key more than once"},
54 // Contract error namespace (3xx)
55 ErrCompileContract: {400, "BTM300", "Compile contract failed"},
56 ErrInstContract: {400, "BTM301", "Instantiate contract failed"},
57 contract.ErrContractDuplicated: {400, "BTM302", "Contract is duplicated"},
58 contract.ErrContractNotFound: {400, "BTM303", "Contract not found"},
60 // Transaction error namespace (7xx)
61 // Build transaction error namespace (70x ~ 72x)
62 account.ErrInsufficient: {400, "BTM700", "Funds of account are insufficient"},
63 account.ErrImmature: {400, "BTM701", "Available funds of account are immature"},
64 account.ErrReserved: {400, "BTM702", "Available UTXOs of account have been reserved"},
65 account.ErrMatchUTXO: {400, "BTM703", "UTXO with given hash not found"},
66 ErrBadActionType: {400, "BTM704", "Invalid action type"},
67 ErrBadAction: {400, "BTM705", "Invalid action object"},
68 ErrBadActionConstruction: {400, "BTM706", "Invalid action construction"},
69 txbuilder.ErrMissingFields: {400, "BTM707", "One or more fields are missing"},
70 txbuilder.ErrBadAmount: {400, "BTM708", "Invalid asset amount"},
71 account.ErrFindAccount: {400, "BTM709", "Account not found"},
72 asset.ErrFindAsset: {400, "BTM710", "Asset not found"},
73 txbuilder.ErrBadContractArgType: {400, "BTM711", "Invalid contract argument type"},
74 txbuilder.ErrOrphanTx: {400, "BTM712", "Transaction input UTXO not found"},
75 txbuilder.ErrExtTxFee: {400, "BTM713", "Transaction fee exceeded max limit"},
76 txbuilder.ErrNoGasInput: {400, "BTM714", "Transaction has no gas input"},
78 // Submit transaction error namespace (73x ~ 79x)
79 // Validation error (73x ~ 75x)
80 validation.ErrTxVersion: {400, "BTM730", "Invalid transaction version"},
81 validation.ErrWrongTransactionSize: {400, "BTM731", "Invalid transaction size"},
82 validation.ErrBadTimeRange: {400, "BTM732", "Invalid transaction time range"},
83 validation.ErrNotStandardTx: {400, "BTM733", "Not standard transaction"},
84 validation.ErrWrongCoinbaseTransaction: {400, "BTM734", "Invalid coinbase transaction"},
85 validation.ErrWrongCoinbaseAsset: {400, "BTM735", "Invalid coinbase assetID"},
86 validation.ErrCoinbaseArbitraryOversize: {400, "BTM736", "Invalid coinbase arbitrary size"},
87 validation.ErrEmptyResults: {400, "BTM737", "No results in the transaction"},
88 validation.ErrMismatchedAssetID: {400, "BTM738", "Mismatched assetID"},
89 validation.ErrMismatchedPosition: {400, "BTM739", "Mismatched value source/dest position"},
90 validation.ErrMismatchedReference: {400, "BTM740", "Mismatched reference"},
91 validation.ErrMismatchedValue: {400, "BTM741", "Mismatched value"},
92 validation.ErrMissingField: {400, "BTM742", "Missing required field"},
93 validation.ErrNoSource: {400, "BTM743", "No source for value"},
94 validation.ErrOverflow: {400, "BTM744", "Arithmetic overflow/underflow"},
95 validation.ErrPosition: {400, "BTM745", "Invalid source or destination position"},
96 validation.ErrUnbalanced: {400, "BTM746", "Unbalanced asset amount between input and output"},
97 validation.ErrOverGasCredit: {400, "BTM747", "Gas credit has been spent"},
98 validation.ErrGasCalculate: {400, "BTM748", "Gas usage calculate got a math error"},
100 // VM error (76x ~ 78x)
101 vm.ErrAltStackUnderflow: {400, "BTM760", "Alt stack underflow"},
102 vm.ErrBadValue: {400, "BTM761", "Bad value"},
103 vm.ErrContext: {400, "BTM762", "Wrong context"},
104 vm.ErrDataStackUnderflow: {400, "BTM763", "Data stack underflow"},
105 vm.ErrDisallowedOpcode: {400, "BTM764", "Disallowed opcode"},
106 vm.ErrDivZero: {400, "BTM765", "Division by zero"},
107 vm.ErrFalseVMResult: {400, "BTM766", "False result for executing VM"},
108 vm.ErrLongProgram: {400, "BTM767", "Program size exceeds max int32"},
109 vm.ErrRange: {400, "BTM768", "Arithmetic range error"},
110 vm.ErrReturn: {400, "BTM769", "RETURN executed"},
111 vm.ErrRunLimitExceeded: {400, "BTM770", "Run limit exceeded because the BTM Fee is insufficient"},
112 vm.ErrShortProgram: {400, "BTM771", "Unexpected end of program"},
113 vm.ErrToken: {400, "BTM772", "Unrecognized token"},
114 vm.ErrUnexpected: {400, "BTM773", "Unexpected error"},
115 vm.ErrUnsupportedVM: {400, "BTM774", "Unsupported VM because the version of VM is mismatched"},
116 vm.ErrVerifyFailed: {400, "BTM775", "VERIFY failed"},
118 // Mock HSM error namespace (8xx)
119 pseudohsm.ErrDuplicateKeyAlias: {400, "BTM800", "Key Alias already exists"},
120 pseudohsm.ErrLoadKey: {400, "BTM801", "Key not found or wrong password"},
121 pseudohsm.ErrDecrypt: {400, "BTM802", "Could not decrypt key with given passphrase"},
124 // Map error values to standard bytom error codes. Missing entries
125 // will map to internalErrInfo.
127 // TODO(jackson): Share one error table across Chain
128 // products/services so that errors are consistent.
129 var errorFormatter = httperror.Formatter{
130 Default: httperror.Info{500, "BTM000", "Bytom API Error"},
131 IsTemporary: isTemporary,
132 Errors: map[error]httperror.Info{
133 // General error namespace (0xx)
134 context.DeadlineExceeded: {408, "BTM001", "Request timed out"},
135 httpjson.ErrBadRequest: {400, "BTM002", "Invalid request body"},
136 rpc.ErrWrongNetwork: {502, "BTM103", "A peer core is operating on a different blockchain network"},
138 //accesstoken authz err namespace (86x)
139 errNotAuthenticated: {401, "BTM860", "Request could not be authenticated"},