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