-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
362 lines (282 loc) · 8.4 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package sqldb
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
var (
_ Connection = connectionWithError{}
_ RowScanner = rowScannerWithError{}
_ RowsScanner = rowsScannerWithError{}
)
// ReplaceErrNoRows returns the passed replacement error
// if errors.Is(err, sql.ErrNoRows),
// else the passed err is returned unchanged.
func ReplaceErrNoRows(err, replacement error) error {
if err != nil && errors.Is(err, sql.ErrNoRows) {
return replacement
}
return err
}
// IsOtherThanErrNoRows returns true if the passed error is not nil
// and does not unwrap to, or is sql.ErrNoRows.
func IsOtherThanErrNoRows(err error) bool {
return err != nil && !errors.Is(err, sql.ErrNoRows)
}
// sentinelError implements the error interface for a string
// and is meant to be used to declare const sentinel errors.
//
// Example:
//
// const ErrUserNotFound impl.sentinelError = "user not found"
type sentinelError string
func (s sentinelError) Error() string {
return string(s)
}
// Transaction errors
const (
// ErrWithinTransaction is returned by methods
// that are not allowed within DB transactions
// when the DB connection is a transaction.
ErrWithinTransaction sentinelError = "within a transaction"
// ErrNotWithinTransaction is returned by methods
// that are are only allowed within DB transactions
// when the DB connection is not a transaction.
ErrNotWithinTransaction sentinelError = "not within a transaction"
ErrNullValueNotAllowed sentinelError = "null value not allowed"
)
type ErrRaisedException struct {
Message string
}
func (e ErrRaisedException) Error() string {
return "raised exception: " + e.Message
}
type ErrIntegrityConstraintViolation struct {
Constraint string
}
func (e ErrIntegrityConstraintViolation) Error() string {
if e.Constraint == "" {
return "integrity constraint violation"
}
return "integrity constraint violation of constraint: " + e.Constraint
}
type ErrRestrictViolation struct {
Constraint string
}
func (e ErrRestrictViolation) Error() string {
if e.Constraint == "" {
return "restrict violation"
}
return "restrict violation of constraint: " + e.Constraint
}
func (e ErrRestrictViolation) Unwrap() error {
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
}
type ErrNotNullViolation struct {
Constraint string
}
func (e ErrNotNullViolation) Error() string {
if e.Constraint == "" {
return "not null violation"
}
return "not null violation of constraint: " + e.Constraint
}
func (e ErrNotNullViolation) Unwrap() error {
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
}
type ErrForeignKeyViolation struct {
Constraint string
}
func (e ErrForeignKeyViolation) Error() string {
if e.Constraint == "" {
return "foreign key violation"
}
return "foreign key violation of constraint: " + e.Constraint
}
func (e ErrForeignKeyViolation) Unwrap() error {
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
}
type ErrUniqueViolation struct {
Constraint string
}
func (e ErrUniqueViolation) Error() string {
if e.Constraint == "" {
return "unique violation"
}
return "unique violation of constraint: " + e.Constraint
}
func (e ErrUniqueViolation) Unwrap() error {
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
}
type ErrCheckViolation struct {
Constraint string
}
func (e ErrCheckViolation) Error() string {
if e.Constraint == "" {
return "check violation"
}
return "check violation of constraint: " + e.Constraint
}
func (e ErrCheckViolation) Unwrap() error {
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
}
type ErrExclusionViolation struct {
Constraint string
}
func (e ErrExclusionViolation) Error() string {
if e.Constraint == "" {
return "exclusion violation"
}
return "exclusion violation of constraint: " + e.Constraint
}
func (e ErrExclusionViolation) Unwrap() error {
return ErrIntegrityConstraintViolation{Constraint: e.Constraint}
}
// ConnectionWithError
// ConnectionWithError returns a dummy Connection
// where all methods return the passed error.
func ConnectionWithError(ctx context.Context, err error) Connection {
if err == nil {
panic("ConnectionWithError needs an error")
}
return connectionWithError{ctx, err}
}
type connectionWithError struct {
ctx context.Context
err error
}
func (e connectionWithError) Context() context.Context { return e.ctx }
func (e connectionWithError) WithContext(ctx context.Context) Connection {
return connectionWithError{ctx: ctx, err: e.err}
}
func (e connectionWithError) WithStructFieldMapper(namer StructFieldMapper) Connection {
return e
}
func (e connectionWithError) StructFieldMapper() StructFieldMapper {
return DefaultStructFieldMapping
}
func (e connectionWithError) Ping(time.Duration) error {
return e.err
}
func (e connectionWithError) Stats() sql.DBStats {
return sql.DBStats{}
}
func (e connectionWithError) Config() *Config {
return &Config{Err: e.err}
}
func (e connectionWithError) Placeholder(paramIndex int) string {
return fmt.Sprintf("$%d", paramIndex+1)
}
func (e connectionWithError) ValidateColumnName(name string) error {
return e.err
}
func (e connectionWithError) Exec(query string, args ...any) error {
return e.err
}
func (e connectionWithError) Update(table string, values Values, where string, args ...any) error {
return e.err
}
func (e connectionWithError) UpdateReturningRow(table string, values Values, returning, where string, args ...any) RowScanner {
return RowScannerWithError(e.err)
}
func (e connectionWithError) UpdateReturningRows(table string, values Values, returning, where string, args ...any) RowsScanner {
return RowsScannerWithError(e.err)
}
func (e connectionWithError) UpdateStruct(table string, rowStruct any, ignoreColumns ...ColumnFilter) error {
return e.err
}
func (e connectionWithError) UpsertStruct(table string, rowStruct any, ignoreColumns ...ColumnFilter) error {
return e.err
}
func (e connectionWithError) QueryRow(query string, args ...any) RowScanner {
return RowScannerWithError(e.err)
}
func (e connectionWithError) QueryRows(query string, args ...any) RowsScanner {
return RowsScannerWithError(e.err)
}
func (e connectionWithError) IsTransaction() bool {
return false
}
func (e connectionWithError) TransactionNo() uint64 {
return 0
}
func (ce connectionWithError) TransactionOptions() (*sql.TxOptions, bool) {
return nil, false
}
func (e connectionWithError) Begin(opts *sql.TxOptions, no uint64) (Connection, error) {
return nil, e.err
}
func (e connectionWithError) Commit() error {
return e.err
}
func (e connectionWithError) Rollback() error {
return e.err
}
func (e connectionWithError) Transaction(opts *sql.TxOptions, txFunc func(tx Connection) error) error {
return e.err
}
func (e connectionWithError) ListenOnChannel(channel string, onNotify OnNotifyFunc, onUnlisten OnUnlistenFunc) error {
return e.err
}
func (e connectionWithError) UnlistenChannel(channel string) error {
return e.err
}
func (e connectionWithError) IsListeningOnChannel(channel string) bool {
return false
}
func (e connectionWithError) Close() error {
return e.err
}
// RowScannerWithError
// RowScannerWithError returns a dummy RowScanner
// where all methods return the passed error.
func RowScannerWithError(err error) RowScanner {
return rowScannerWithError{err}
}
type rowScannerWithError struct {
err error
}
func (e rowScannerWithError) Scan(dest ...any) error {
return e.err
}
func (e rowScannerWithError) ScanStruct(dest any) error {
return e.err
}
func (e rowScannerWithError) ScanValues() ([]any, error) {
return nil, e.err
}
func (e rowScannerWithError) ScanStrings() ([]string, error) {
return nil, e.err
}
func (e rowScannerWithError) Columns() ([]string, error) {
return nil, e.err
}
// RowsScannerWithError
// RowsScannerWithError returns a dummy RowsScanner
// where all methods return the passed error.
func RowsScannerWithError(err error) RowsScanner {
return rowsScannerWithError{err}
}
type rowsScannerWithError struct {
err error
}
func (e rowsScannerWithError) ScanSlice(dest any) error {
return e.err
}
func (e rowsScannerWithError) ScanStructSlice(dest any) error {
return e.err
}
func (e rowsScannerWithError) Columns() ([]string, error) {
return nil, e.err
}
func (e rowsScannerWithError) ScanAllRowsAsStrings(headerRow bool) ([][]string, error) {
return nil, e.err
}
func (e rowsScannerWithError) ForEachRow(callback func(RowScanner) error) error {
return e.err
}
func (e rowsScannerWithError) ForEachRowCall(callback any) error {
return e.err
}