github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcjson/error.go (about)

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