github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/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/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"
    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  	contract.ErrContractDuplicated: {400, "BTM302", "Contract is duplicated"},
    56  	contract.ErrContractNotFound:   {400, "BTM303", "Contract not found"},
    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  	ErrBadActionType:                {400, "BTM704", "Invalid action type"},
    65  	ErrBadAction:                    {400, "BTM705", "Invalid action object"},
    66  	ErrBadActionConstruction:        {400, "BTM706", "Invalid action construction"},
    67  	txbuilder.ErrMissingFields:      {400, "BTM707", "One or more fields are missing"},
    68  	txbuilder.ErrBadAmount:          {400, "BTM708", "Invalid asset amount"},
    69  	account.ErrFindAccount:          {400, "BTM709", "Account not found"},
    70  	asset.ErrFindAsset:              {400, "BTM710", "Asset not found"},
    71  	txbuilder.ErrBadContractArgType: {400, "BTM711", "Invalid contract argument type"},
    72  	txbuilder.ErrOrphanTx:           {400, "BTM712", "Transaction input UTXO not found"},
    73  	txbuilder.ErrExtTxFee:           {400, "BTM713", "Transaction fee exceeded max limit"},
    74  	txbuilder.ErrNoGasInput:         {400, "BTM714", "Transaction has no gas input"},
    75  
    76  	// Submit transaction error namespace (73x ~ 79x)
    77  	// Validation error (73x ~ 75x)
    78  	validation.ErrTxVersion:                 {400, "BTM730", "Invalid transaction version"},
    79  	validation.ErrWrongTransactionSize:      {400, "BTM731", "Invalid transaction size"},
    80  	validation.ErrBadTimeRange:              {400, "BTM732", "Invalid transaction time range"},
    81  	validation.ErrNotStandardTx:             {400, "BTM733", "Not standard transaction"},
    82  	validation.ErrWrongCoinbaseTransaction:  {400, "BTM734", "Invalid coinbase transaction"},
    83  	validation.ErrWrongCoinbaseAsset:        {400, "BTM735", "Invalid coinbase assetID"},
    84  	validation.ErrCoinbaseArbitraryOversize: {400, "BTM736", "Invalid coinbase arbitrary size"},
    85  	validation.ErrEmptyResults:              {400, "BTM737", "No results in the transaction"},
    86  	validation.ErrMismatchedAssetID:         {400, "BTM738", "Mismatched assetID"},
    87  	validation.ErrMismatchedPosition:        {400, "BTM739", "Mismatched value source/dest position"},
    88  	validation.ErrMismatchedReference:       {400, "BTM740", "Mismatched reference"},
    89  	validation.ErrMismatchedValue:           {400, "BTM741", "Mismatched value"},
    90  	validation.ErrMissingField:              {400, "BTM742", "Missing required field"},
    91  	validation.ErrNoSource:                  {400, "BTM743", "No source for value"},
    92  	validation.ErrOverflow:                  {400, "BTM744", "Arithmetic overflow/underflow"},
    93  	validation.ErrPosition:                  {400, "BTM745", "Invalid source or destination position"},
    94  	validation.ErrUnbalanced:                {400, "BTM746", "Unbalanced asset amount between input and output"},
    95  	validation.ErrOverGasCredit:             {400, "BTM747", "Gas credit has been spent"},
    96  	validation.ErrGasCalculate:              {400, "BTM748", "Gas usage calculate got a math error"},
    97  
    98  	// VM error (76x ~ 78x)
    99  	vm.ErrAltStackUnderflow:  {400, "BTM760", "Alt stack underflow"},
   100  	vm.ErrBadValue:           {400, "BTM761", "Bad value"},
   101  	vm.ErrContext:            {400, "BTM762", "Wrong context"},
   102  	vm.ErrDataStackUnderflow: {400, "BTM763", "Data stack underflow"},
   103  	vm.ErrDisallowedOpcode:   {400, "BTM764", "Disallowed opcode"},
   104  	vm.ErrDivZero:            {400, "BTM765", "Division by zero"},
   105  	vm.ErrFalseVMResult:      {400, "BTM766", "False result for executing VM"},
   106  	vm.ErrLongProgram:        {400, "BTM767", "Program size exceeds max int32"},
   107  	vm.ErrRange:              {400, "BTM768", "Arithmetic range error"},
   108  	vm.ErrReturn:             {400, "BTM769", "RETURN executed"},
   109  	vm.ErrRunLimitExceeded:   {400, "BTM770", "Run limit exceeded because the BTM Fee is insufficient"},
   110  	vm.ErrShortProgram:       {400, "BTM771", "Unexpected end of program"},
   111  	vm.ErrToken:              {400, "BTM772", "Unrecognized token"},
   112  	vm.ErrUnexpected:         {400, "BTM773", "Unexpected error"},
   113  	vm.ErrUnsupportedVM:      {400, "BTM774", "Unsupported VM because the version of VM is mismatched"},
   114  	vm.ErrVerifyFailed:       {400, "BTM775", "VERIFY failed"},
   115  
   116  	// Mock HSM error namespace (8xx)
   117  	pseudohsm.ErrDuplicateKeyAlias: {400, "BTM800", "Key Alias already exists"},
   118  	pseudohsm.ErrLoadKey:           {400, "BTM801", "Key not found or wrong password"},
   119  	pseudohsm.ErrDecrypt:           {400, "BTM802", "Could not decrypt key with given passphrase"},
   120  }
   121  
   122  // Map error values to standard bytom error codes. Missing entries
   123  // will map to internalErrInfo.
   124  //
   125  // TODO(jackson): Share one error table across Chain
   126  // products/services so that errors are consistent.
   127  var errorFormatter = httperror.Formatter{
   128  	Default:     httperror.Info{500, "BTM000", "Bytom API Error"},
   129  	IsTemporary: isTemporary,
   130  	Errors: map[error]httperror.Info{
   131  		// General error namespace (0xx)
   132  		context.DeadlineExceeded: {408, "BTM001", "Request timed out"},
   133  		httpjson.ErrBadRequest:   {400, "BTM002", "Invalid request body"},
   134  		rpc.ErrWrongNetwork:      {502, "BTM103", "A peer core is operating on a different blockchain network"},
   135  
   136  		//accesstoken authz err namespace (86x)
   137  		errNotAuthenticated: {401, "BTM860", "Request could not be authenticated"},
   138  	},
   139  }