/
errors.go
151 lines (129 loc) · 4.19 KB
/
errors.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package db
import "errors"
// TODO: Consider changing error types to Error and DetailedError for direct
// errors package unwrapping and tests with Is/As:
// Error is just a basic error.
//type Error string
// Error satisfies the error interface.
// func (e Error) Error() string {
// return string(e)
// }
// DetailedError pairs an Error with details.
// type DetailedError struct {
// wrapped Error
// detail string
// }
// Error satisfies the error interface, combining the wrapped error message with
// the details.
// func (e DetailedError) Error() string {
// return e.wrapped.Error() + ": " + e.detail
// }
// NewDetailedError wraps the provided Error with details in a DetailedError,
// facilitating the use of errors.Is and errors.As via errors.Unwrap.
// func NewDetailedError(err Error, detail string) DetailedError {
// return DetailedError{
// wrapped: err,
// detail: detail,
// }
// }
// ArchiveError is the error type used by archivist for certain recognized
// errors. Not all returned errors will be of this type.
type ArchiveError struct {
Code uint16
Detail string
}
// The possible Code values in an ArchiveError.
const (
ErrGeneralFailure uint16 = iota
ErrUnknownMatch
ErrUnknownOrder
ErrUnsupportedMarket
ErrInvalidOrder
ErrReusedCommit
ErrOrderNotExecuted
ErrUpdateCount
)
func (ae ArchiveError) Error() string {
desc := "unrecognized error"
switch ae.Code {
case ErrGeneralFailure:
desc = "general failure"
case ErrUnknownMatch:
desc = "unknown match"
case ErrUnknownOrder:
desc = "unknown order"
case ErrUnsupportedMarket:
desc = "unsupported market"
case ErrInvalidOrder:
desc = "invalid order"
case ErrReusedCommit:
desc = "order commit reused"
case ErrOrderNotExecuted:
desc = "order not in executed status"
case ErrUpdateCount:
desc = "unexpected number of rows updated"
}
if ae.Detail == "" {
return desc
}
return desc + ": " + ae.Detail
}
// SameErrorTypes checks for error equality or ArchiveError.Code equality if
// both errors are of type ArchiveError.
func SameErrorTypes(errA, errB error) bool {
var arA, arB ArchiveError
if errors.As(errA, &arA) && errors.As(errB, &arB) {
return arA.Code == arB.Code
}
return errors.Is(errA, errB)
}
// IsErrGeneralFailure returns true if the error is of type ArchiveError and has
// code ErrGeneralFailure.
func IsErrGeneralFailure(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrGeneralFailure
}
// IsErrOrderUnknown returns true if the error is of type ArchiveError and has
// code ErrUnknownOrder.
func IsErrOrderUnknown(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrUnknownOrder
}
// IsErrMatchUnknown returns true if the error is of type ArchiveError and has
// code ErrUnknownMatch.
func IsErrMatchUnknown(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrUnknownMatch
}
// IsErrMatchUnsupported returns true if the error is of type ArchiveError and
// has code ErrUnsupportedMarket.
func IsErrMatchUnsupported(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrUnsupportedMarket
}
// IsErrInvalidOrder returns true if the error is of type ArchiveError and has
// code ErrInvalidOrder.
func IsErrInvalidOrder(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrInvalidOrder
}
// IsErrReusedCommit returns true if the error is of type ArchiveError and has
// code ErrReusedCommit.
func IsErrReusedCommit(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrReusedCommit
}
// IsErrOrderNotExecuted returns true if the error is of type ArchiveError and
// has code ErrOrderNotExecuted.
func IsErrOrderNotExecuted(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrOrderNotExecuted
}
// IsErrUpdateCount returns true if the error is of type ArchiveError and has
// code ErrUpdateCount.
func IsErrUpdateCount(err error) bool {
var errA ArchiveError
return errors.As(err, &errA) && errA.Code == ErrUpdateCount
}