error.go raw

   1  package btcjson
   2  
   3  import (
   4  	"fmt"
   5  )
   6  
   7  // ErrorCode identifies a kind of error.  These error codes are NOT used for
   8  // JSON-RPC response errors.
   9  type ErrorCode int
  10  
  11  // These constants are used to identify a specific RuleError.
  12  const (
  13  	// ErrDuplicateMethod indicates a command with the specified method already exists.
  14  	ErrDuplicateMethod ErrorCode = iota
  15  	// ErrInvalidUsageFlags indicates one or more unrecognized flag bits were specified.
  16  	ErrInvalidUsageFlags
  17  	// ErrInvalidType indicates a type was passed that is not the required type.
  18  	ErrInvalidType
  19  	// ErrEmbeddedType indicates the provided command struct contains an embedded type which is not not supported.
  20  	ErrEmbeddedType
  21  	// ErrUnexportedField indiciates the provided command struct contains an unexported field which is not supported.
  22  	ErrUnexportedField
  23  	// ErrUnsupportedFieldType indicates the type of a field in the provided command struct is not one of the supported
  24  	// types.
  25  	ErrUnsupportedFieldType
  26  	// ErrNonOptionalField indicates a non-optional field was specified after an optional field.
  27  	ErrNonOptionalField
  28  	// ErrNonOptionalDefault indicates a 'jsonrpcdefault' struct tag was specified for a non-optional field.
  29  	ErrNonOptionalDefault
  30  	// ErrMismatchedDefault indicates a 'jsonrpcdefault' struct tag contains a value that doesn't match the type of the
  31  	// field.
  32  	ErrMismatchedDefault
  33  	// ErrUnregisteredMethod indicates a method was specified that has not been registered.
  34  	ErrUnregisteredMethod
  35  	// ErrMissingDescription indicates a description required to generate help is missing.
  36  	ErrMissingDescription
  37  	// ErrNumParams indicates the number of netparams supplied do not match the requirements of the associated command.
  38  	ErrNumParams
  39  	// numErrorCodes is the maximum error code number used in tests.
  40  	numErrorCodes
  41  )
  42  
  43  // Map of ErrorCode values back to their constant names for pretty printing.
  44  var errorCodeStrings = map[ErrorCode]string{
  45  	ErrDuplicateMethod:      "ErrDuplicateMethod",
  46  	ErrInvalidUsageFlags:    "ErrInvalidUsageFlags",
  47  	ErrInvalidType:          "ErrInvalidType",
  48  	ErrEmbeddedType:         "ErrEmbeddedType",
  49  	ErrUnexportedField:      "ErrUnexportedField",
  50  	ErrUnsupportedFieldType: "ErrUnsupportedFieldType",
  51  	ErrNonOptionalField:     "ErrNonOptionalField",
  52  	ErrNonOptionalDefault:   "ErrNonOptionalDefault",
  53  	ErrMismatchedDefault:    "ErrMismatchedDefault",
  54  	ErrUnregisteredMethod:   "ErrUnregisteredMethod",
  55  	ErrMissingDescription:   "ErrMissingDescription",
  56  	ErrNumParams:            "ErrNumParams",
  57  }
  58  
  59  // String returns the ErrorCode as a human-readable name.
  60  func (e ErrorCode) String() string {
  61  	if s := errorCodeStrings[e]; s != "" {
  62  		return s
  63  	}
  64  	return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
  65  }
  66  
  67  // GeneralError identifies a general error. This differs from an RPCError in that this error typically is used more by
  68  // the consumers of the package as opposed to RPCErrors which are intended to be returned to the client across the wire
  69  // via a JSON-RPC Response. The caller can use type assertions to determine the specific error and access the ErrorCode
  70  // field.
  71  type GeneralError struct {
  72  	ErrorCode   ErrorCode // Describes the kind of error
  73  	Description string    // Human readable description of the issue
  74  }
  75  
  76  // BTCJSONError satisfies the error interface and prints human-readable errors.
  77  func (e GeneralError) Error() string {
  78  	return e.Description
  79  }
  80  
  81  // makeError creates an BTCJSONError given a set of arguments.
  82  func makeError(c ErrorCode, desc string) GeneralError {
  83  	return GeneralError{ErrorCode: c, Description: desc}
  84  }
  85