github.com/lbryio/lbcd@v0.22.119/btcjson/error.go (about)

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