github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/errors/base.go (about)

     1  package errors
     2  
     3  import (
     4  	"github.com/onflow/cadence/runtime"
     5  
     6  	"github.com/onflow/flow-go/model/flow"
     7  )
     8  
     9  // NewInvalidAddressErrorf constructs a new CodedError which indicates that a
    10  // transaction references an invalid flow Address in either the Authorizers or
    11  // Payer field.
    12  func NewInvalidAddressErrorf(
    13  	address flow.Address,
    14  	msg string,
    15  	args ...interface{},
    16  ) CodedError {
    17  	return NewCodedError(
    18  		ErrCodeInvalidAddressError,
    19  		"invalid address (%s): "+msg,
    20  		append([]interface{}{address.String()}, args...)...)
    21  }
    22  
    23  // NewInvalidArgumentErrorf constructs a new CodedError which indicates that a
    24  // transaction includes invalid arguments. This error is the result of failure
    25  // in any of the following conditions:
    26  // - number of arguments doesn't match the template
    27  //
    28  // TODO add more cases like argument size
    29  func NewInvalidArgumentErrorf(msg string, args ...interface{}) CodedError {
    30  	return NewCodedError(
    31  		ErrCodeInvalidArgumentError,
    32  		"transaction arguments are invalid: ("+msg+")",
    33  		args...)
    34  }
    35  
    36  func IsInvalidArgumentError(err error) bool {
    37  	return HasErrorCode(err, ErrCodeInvalidArgumentError)
    38  }
    39  
    40  // NewInvalidLocationErrorf constructs a new CodedError which indicates an
    41  // invalid location is passed.
    42  func NewInvalidLocationErrorf(
    43  	location runtime.Location,
    44  	msg string,
    45  	args ...interface{},
    46  ) CodedError {
    47  	locationStr := ""
    48  	if location != nil {
    49  		locationStr = location.String()
    50  	}
    51  
    52  	return NewCodedError(
    53  		ErrCodeInvalidLocationError,
    54  		"location (%s) is not a valid location: "+msg,
    55  		append([]interface{}{locationStr}, args...)...)
    56  }
    57  
    58  // NewValueErrorf constructs a new CodedError which indicates a value is not
    59  // valid value.
    60  func NewValueErrorf(
    61  	valueStr string,
    62  	msg string,
    63  	args ...interface{},
    64  ) CodedError {
    65  	return NewCodedError(
    66  		ErrCodeValueError,
    67  		"invalid value (%s): "+msg,
    68  		append([]interface{}{valueStr}, args...)...)
    69  }
    70  
    71  func IsValueError(err error) bool {
    72  	return HasErrorCode(err, ErrCodeValueError)
    73  }
    74  
    75  // NewOperationAuthorizationErrorf constructs a new CodedError which indicates
    76  // not enough authorization to perform an operations like account creation or
    77  // smart contract deployment.
    78  func NewOperationAuthorizationErrorf(
    79  	operation string,
    80  	msg string,
    81  	args ...interface{},
    82  ) CodedError {
    83  	return NewCodedError(
    84  		ErrCodeOperationAuthorizationError,
    85  		"(%s) is not authorized: "+msg,
    86  		append([]interface{}{operation}, args...)...)
    87  }
    88  
    89  // NewAccountAuthorizationErrorf constructs a new CodedError which indicates
    90  // that an authorization issue either:
    91  //   - a transaction is missing a required signature to authorize access to an
    92  //     account, or
    93  //   - a transaction doesn't have authorization to performe some operations like
    94  //     account creation.
    95  func NewAccountAuthorizationErrorf(
    96  	address flow.Address,
    97  	msg string,
    98  	args ...interface{},
    99  ) CodedError {
   100  	return NewCodedError(
   101  		ErrCodeAccountAuthorizationError,
   102  		"authorization failed for account %s: "+msg,
   103  		append([]interface{}{address}, args...)...)
   104  }