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