-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
118 lines (94 loc) · 3.33 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
package lfsb
import (
"errors"
"fmt"
"github.com/superfly/ltx"
)
type ErrorType string
const (
ErrorTypeAuth ErrorType = "auth"
ErrorTypeConflict ErrorType = "conflict"
ErrorTypeNotFound ErrorType = "notfound"
ErrorTypeValidation ErrorType = "validation"
ErrorTypeUnprocessable ErrorType = "unprocessable"
ErrorTypeUnknown = "unknown"
)
type Error struct {
Type ErrorType
Code string
Message string
TXID ltx.TXID
PostApplyChecksum ltx.Pos
}
// Error implements Error interace
func (e *Error) Error() string {
return fmt.Sprintf("%s (%s): %s", e.Type, e.Code, e.Message)
}
// Errorf is a helper function for returning Error values.
func Errorf(typ ErrorType, code, format string, a ...any) *Error {
return &Error{
Type: typ,
Code: code,
Message: fmt.Sprintf(format, a...),
}
}
func (e *Error) Is(err error) bool {
if re, ok := err.(*Error); ok {
return e.Type == re.Type && e.Code == re.Code && e.Message == re.Message
}
return false
}
// ErrorCode returns the error code from an error. Returns blank if err is nil.
// Returns EINTERNAL if no lfsc.Error is found.
func ErrorCode(err error) string {
if err == nil {
return ""
}
for {
switch x := err.(type) {
case *Error:
return x.Code
case interface{ Unwrap() error }:
err = x.Unwrap()
default:
return "EINTERNAL"
}
}
}
// Common error codes. Used for compile-time checks.
const (
EPOSMISMATCH = "EPOSMISMATCH"
ENOCLUSTER = "ENOCLUSTER"
ENOCOMPACTION = "ENOCOMPACTION"
EPARTIALCOMPACTION = "EPARTIALCOMPACTION"
)
var (
ErrInvalidLTXFilename = Errorf(ErrorTypeValidation, "EBADPATH", "invalid ltx filename")
ErrClusterRequired = Errorf(ErrorTypeValidation, "EBADCLUSTER", "cluster required")
ErrClusterInvalid = Errorf(ErrorTypeValidation, "EBADCLUSTER", "cluster invalid")
ErrDatabaseRequired = Errorf(ErrorTypeValidation, "EBADDB", "database required")
ErrDatabaseNotFound = Errorf(ErrorTypeNotFound, "ENODB", "database not found")
ErrMinTXIDRequired = Errorf(ErrorTypeValidation, "EBADTXID", "minimum transaction id required")
ErrMaxTXIDRequired = Errorf(ErrorTypeValidation, "EBADTXID", "maximum transaction id required")
// ErrInvalidPos = Errorf(ErrorTypeValidation, "EBADPOS", "DB position is invalid")
ErrCannotCompactToLevelZero = Errorf(ErrorTypeValidation, "EBADLEVEL", "cannot compact to level zero")
ErrCompactionLevelTooHigh = Errorf(ErrorTypeValidation, "EBADLEVEL", "compaction level too high")
ErrTxNotAvailable = Errorf(ErrorTypeNotFound, "ENOTXID", "tx not available")
// ErrPgnoOutOfBounds = Errorf(ErrorTypeNotFound, "EPGNOOOB", "page number out of bounds")
ErrPageNotFound = Errorf(ErrorTypeNotFound, "ENOPAGE", "page not found")
// ErrInvalidPgno = Errorf(ErrorTypeValidation, "EINVALIDPGNO", "invalid page number")
ErrTimestampNotAvailable = Errorf(ErrorTypeNotFound, "ENOTIMESTAMP", "timestamp not available")
ErrPageSizeMismatch = Errorf(ErrorTypeUnprocessable, "EBADHEADER", "page size mismatch")
)
// IsApplicationError returns true if err is an lfsc.Error or ltx.PositionMismatchError.
func IsApplicationError(err error) bool {
if err == nil {
return false
}
var lfscError *Error
if errors.As(err, &lfscError) {
return true
}
var posMismatchError *ltx.PosMismatchError
return errors.As(err, &posMismatchError)
}