OSDN Git Service

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