OSDN Git Service

Merge pull request #674 from Bytom/dev_addr_pre_for_net
[bytom/bytom.git] / api / errors.go
1 package api
2
3 import (
4         "context"
5
6         "github.com/bytom/account"
7         "github.com/bytom/blockchain/query"
8         "github.com/bytom/blockchain/query/filter"
9         "github.com/bytom/blockchain/rpc"
10         "github.com/bytom/blockchain/signers"
11         "github.com/bytom/blockchain/txbuilder"
12         "github.com/bytom/errors"
13         "github.com/bytom/net/http/httperror"
14         "github.com/bytom/net/http/httpjson"
15         "github.com/bytom/protocol"
16 )
17
18 func isTemporary(info httperror.Info, err error) bool {
19         switch info.ChainCode {
20         case "BTM000": // internal server error
21                 return true
22         case "BTM001": // request timed out
23                 return true
24         case "BTM761": // outputs currently reserved
25                 return true
26         case "BTM706": // 1 or more action errors
27                 errs := errors.Data(err)["actions"].([]httperror.Response)
28                 temp := true
29                 for _, actionErr := range errs {
30                         temp = temp && isTemporary(actionErr.Info, nil)
31                 }
32                 return temp
33         default:
34                 return false
35         }
36 }
37
38 // Map error values to standard bytom error codes. Missing entries
39 // will map to internalErrInfo.
40 //
41 // TODO(jackson): Share one error table across Chain
42 // products/services so that errors are consistent.
43 var errorFormatter = httperror.Formatter{
44         Default:     httperror.Info{500, "BTM000", "Bytom API Error"},
45         IsTemporary: isTemporary,
46         Errors: map[error]httperror.Info{
47                 // General error namespace (0xx)
48                 context.DeadlineExceeded:     {408, "BTM001", "Request timed out"},
49                 httpjson.ErrBadRequest:       {400, "BTM003", "Invalid request body"},
50                 txbuilder.ErrMissingFields:   {400, "BTM010", "One or more fields are missing"},
51                 rpc.ErrWrongNetwork:          {502, "BTM104", "A peer core is operating on a different blockchain network"},
52                 protocol.ErrTheDistantFuture: {400, "BTM105", "Requested height is too far ahead"},
53
54                 // Signers error namespace (2xx)
55                 signers.ErrBadQuorum: {400, "BTM200", "Quorum must be greater than 1 and less than or equal to the length of xpubs"},
56                 signers.ErrBadXPub:   {400, "BTM201", "Invalid xpub format"},
57                 signers.ErrNoXPubs:   {400, "BTM202", "At least one xpub is required"},
58                 signers.ErrBadType:   {400, "BTM203", "Retrieved type does not match expected type"},
59                 signers.ErrDupeXPub:  {400, "BTM204", "Root XPubs cannot contain the same key more than once"},
60
61                 // Query error namespace (6xx)
62                 query.ErrBadAfter:               {400, "BTM600", "Malformed pagination parameter `after`"},
63                 query.ErrParameterCountMismatch: {400, "BTM601", "Incorrect number of parameters to filter"},
64                 filter.ErrBadFilter:             {400, "BTM602", "Malformed query filter"},
65
66                 // Transaction error namespace (7xx)
67                 // Build error namespace (70x)
68                 txbuilder.ErrBadRefData: {400, "BTM700", "Reference data does not match previous transaction's reference data"},
69                 txbuilder.ErrBadAmount:  {400, "BTM704", "Invalid asset amount"},
70                 txbuilder.ErrBlankCheck: {400, "BTM705", "Unsafe transaction: leaves assets to be taken without requiring payment"},
71                 txbuilder.ErrAction:     {400, "BTM706", "One or more actions had an error: see attached data"},
72
73                 // Submit error namespace (73x)
74                 txbuilder.ErrMissingRawTx:          {400, "BTM730", "Missing raw transaction"},
75                 txbuilder.ErrBadInstructionCount:   {400, "BTM731", "Too many signing instructions in template for transaction"},
76                 txbuilder.ErrBadTxInputIdx:         {400, "BTM732", "Invalid transaction input index"},
77                 txbuilder.ErrBadWitnessComponent:   {400, "BTM733", "Invalid witness component"},
78                 txbuilder.ErrRejected:              {400, "BTM735", "Transaction rejected"},
79                 txbuilder.ErrNoTxSighashCommitment: {400, "BTM736", "Transaction is not final, additional actions still allowed"},
80                 txbuilder.ErrTxSignatureFailure:    {400, "BTM737", "Transaction signature missing, client may be missing signature key"},
81                 txbuilder.ErrNoTxSighashAttempt:    {400, "BTM738", "Transaction signature was not attempted"},
82
83                 // account action error namespace (76x)
84                 account.ErrInsufficient: {400, "BTM760", "Insufficient funds for tx"},
85                 account.ErrReserved:     {400, "BTM761", "Some outputs are reserved; try again"},
86
87                 //accesstoken authz err namespace (86x)
88                 errNotAuthenticated: {401, "BTM860", "Request could not be authenticated"},
89         },
90 }