-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors.go
193 lines (148 loc) · 5.23 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package docdb
import (
"database/sql"
"errors"
"fmt"
"os"
"github.com/domonda/go-errs"
"github.com/domonda/go-types/uu"
)
// ErrNoChanges is returned when a new document
// version has no changes compared to the previous version.
const ErrNoChanges errs.Sentinel = "no changes"
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentNotFound
type ErrDocumentNotFound struct {
docID uu.ID
}
func NewErrDocumentNotFound(docID uu.ID) ErrDocumentNotFound {
return ErrDocumentNotFound{docID}
}
func (e ErrDocumentNotFound) Error() string {
return fmt.Sprintf("document %s not found", e.docID)
}
func (ErrDocumentNotFound) Is(target error) bool {
return target == os.ErrNotExist || target == sql.ErrNoRows || target == errs.ErrNotFound
}
func (e ErrDocumentNotFound) DocID() uu.ID {
return e.docID
}
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentFileNotFound
type ErrDocumentFileNotFound struct {
docID uu.ID
filename string
}
func NewErrDocumentFileNotFound(docID uu.ID, filename string) ErrDocumentFileNotFound {
return ErrDocumentFileNotFound{docID, filename}
}
func (e ErrDocumentFileNotFound) Error() string {
return fmt.Sprintf("document %s file not found: %q", e.docID, e.filename)
}
func (ErrDocumentFileNotFound) Is(target error) bool {
return target == errs.ErrNotFound || target == os.ErrNotExist
}
func (e ErrDocumentFileNotFound) DocID() uu.ID { return e.docID }
func (e ErrDocumentFileNotFound) Filename() string { return e.filename }
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentVersionNotFound
type ErrDocumentVersionNotFound struct {
docID uu.ID
version VersionTime
}
func NewErrDocumentVersionNotFound(docID uu.ID, version VersionTime) ErrDocumentVersionNotFound {
return ErrDocumentVersionNotFound{docID, version}
}
func (e ErrDocumentVersionNotFound) Error() string {
return fmt.Sprintf("document %s version %s not found", e.docID, e.version)
}
func (ErrDocumentVersionNotFound) Is(target error) bool { return target == errs.ErrNotFound }
func (e ErrDocumentVersionNotFound) DocID() uu.ID { return e.docID }
func (e ErrDocumentVersionNotFound) Version() VersionTime { return e.version }
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentAlreadyExists
type ErrDocumentAlreadyExists struct {
docID uu.ID
}
func NewErrDocumentAlreadyExists(docID uu.ID) ErrDocumentAlreadyExists {
return ErrDocumentAlreadyExists{docID}
}
func (e ErrDocumentAlreadyExists) Error() string {
return fmt.Sprintf("document %s already exists", e.docID)
}
///////////////////////////////////////////////////////////////////////////////
// ErrVersionAlreadyExists
type ErrVersionAlreadyExists struct {
docID uu.ID
version VersionTime
}
func NewErrVersionAlreadyExists(docID uu.ID, version VersionTime) ErrVersionAlreadyExists {
return ErrVersionAlreadyExists{docID, version}
}
func (e ErrVersionAlreadyExists) Error() string {
return fmt.Sprintf("document %s version %s already exists", e.docID, e.version)
}
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentHasNoCommitedVersion
type ErrDocumentHasNoCommitedVersion struct {
docID uu.ID
}
func NewErrDocumentHasNoCommitedVersion(docID uu.ID) ErrDocumentHasNoCommitedVersion {
return ErrDocumentHasNoCommitedVersion{docID}
}
func (e ErrDocumentHasNoCommitedVersion) Error() string {
return fmt.Sprintf("document %s has no commited version yet", e.docID)
}
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentNotCheckedOut
type ErrDocumentNotCheckedOut struct {
docID uu.ID
}
func NewErrDocumentNotCheckedOut(docID uu.ID) ErrDocumentNotCheckedOut {
return ErrDocumentNotCheckedOut{docID}
}
func (e ErrDocumentNotCheckedOut) Error() string {
return fmt.Sprintf("document %s not checked out", e.docID)
}
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentCheckedOut
type ErrDocumentCheckedOut struct {
status *CheckOutStatus
}
func NewErrDocumentCheckedOut(status *CheckOutStatus) ErrDocumentCheckedOut {
if status == nil {
panic("nil CheckOutStatus")
}
return ErrDocumentCheckedOut{status}
}
func (e ErrDocumentCheckedOut) Error() string {
return fmt.Sprintf(
"document %s version %s already checked out by %s for %s at %s",
e.status.DocID,
e.status.Version,
e.status.UserID,
e.status.Reason,
e.status.Time,
)
}
func (e ErrDocumentCheckedOut) CheckOutUserID() uu.ID { return e.status.UserID }
func (e ErrDocumentCheckedOut) CheckOutReason() string { return e.status.Reason }
func IsErrDocumentCheckedOutByUser(err error, userID uu.ID) bool {
var e ErrDocumentCheckedOut
if errors.As(err, &e) {
return e.status.UserID == userID
}
return false
}
///////////////////////////////////////////////////////////////////////////////
// ErrDocumentChanged
type ErrDocumentChanged struct {
docID uu.ID
baseVersion VersionTime
}
func NewErrDocumentChanged(docID uu.ID, version VersionTime) ErrDocumentChanged {
return ErrDocumentChanged{docID, version}
}
func (e ErrDocumentChanged) Error() string {
return fmt.Sprintf("document %s version %s already exists", e.docID, e.baseVersion)
}