-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
95 lines (77 loc) · 3.02 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
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package ticketdb
import (
"fmt"
)
// ErrorCode identifies a kind of error.
type ErrorCode int
// These constants are used to identify a specific RuleError.
const (
// ErrUndoDataShortRead indicates that the given undo serialized data
// was took small.
ErrUndoDataShortRead = iota
// ErrUndoDataNoEntries indicates that the data for undoing ticket data
// in a serialized entry was corrupt.
ErrUndoDataCorrupt
// ErrTicketHashesShortRead indicates that the given ticket hashes
// serialized data was took small.
ErrTicketHashesShortRead
// ErrTicketHashesCorrupt indicates that the data for ticket hashes
// in a serialized entry was corrupt.
ErrTicketHashesCorrupt
// ErrUninitializedBucket indicates that a database bucket was not
// initialized and therefore could not be written to or read from.
ErrUninitializedBucket
// ErrMissingKey indicates that a key was not found in a bucket.
ErrMissingKey
// ErrChainStateShortRead indicates that the given chain state data
// was too small.
ErrChainStateShortRead
// ErrDatabaseInfoShortRead indicates that the given database information
// was too small.
ErrDatabaseInfoShortRead
// ErrLoadAllTickets indicates that there was an error loading the tickets
// from the database, presumably at startup.
ErrLoadAllTickets
)
// Map of ErrorCode values back to their constant names for pretty printing.
var errorCodeStrings = map[ErrorCode]string{
ErrUndoDataShortRead: "ErrUndoDataShortRead",
ErrUndoDataCorrupt: "ErrUndoDataCorrupt",
ErrTicketHashesShortRead: "ErrTicketHashesShortRead",
ErrTicketHashesCorrupt: "ErrTicketHashesCorrupt",
ErrUninitializedBucket: "ErrUninitializedBucket",
ErrMissingKey: "ErrMissingKey",
ErrChainStateShortRead: "ErrChainStateShortRead",
ErrDatabaseInfoShortRead: "ErrDatabaseInfoShortRead",
ErrLoadAllTickets: "ErrLoadAllTickets",
}
// 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))
}
// DBError identifies an error in the stake database for tickets.
// The caller can use type assertions to determine if a failure was
// specifically due to a rule violation and access the ErrorCode field to
// ascertain the specific reason for the rule violation.
type DBError struct {
ErrorCode ErrorCode // Describes the kind of error
Description string // Human readable description of the issue
}
// Error satisfies the error interface and prints human-readable errors.
func (e DBError) Error() string {
return e.Description
}
// GetCode satisfies the error interface and prints human-readable errors.
func (e DBError) GetCode() ErrorCode {
return e.ErrorCode
}
// DBError creates an DBError given a set of arguments.
func ticketDBError(c ErrorCode, desc string) DBError {
return DBError{ErrorCode: c, Description: desc}
}