-
Notifications
You must be signed in to change notification settings - Fork 402
/
gen.go
78 lines (63 loc) · 1.85 KB
/
gen.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package dbx
import (
"context"
"fmt"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"storj.io/storj/private/dbutil/cockroachutil"
"storj.io/storj/private/dbutil/txutil"
"storj.io/storj/private/tagsql"
)
//go:generate sh gen.sh
var mon = monkit.Package()
func init() {
// catch dbx errors
class := errs.Class("satellitedb")
WrapErr = func(e *Error) error {
switch e.Code {
case ErrorCode_NoRows:
return e.Err
case ErrorCode_ConstraintViolation:
return class.Wrap(&constraintError{e.Constraint, e.Err})
}
return class.Wrap(e)
}
ShouldRetry = func(driver string, err error) bool {
if driver == "pgxcockroach" || driver == "cockroach" {
return cockroachutil.NeedsRetry(err)
}
return false
}
}
// Unwrap returns the underlying error.
func (e *Error) Unwrap() error { return e.Err }
// Cause returns the underlying error.
func (e *Error) Cause() error { return e.Err }
type constraintError struct {
constraint string
err error
}
// Unwrap returns the underlying error.
func (err *constraintError) Unwrap() error { return err.err }
// Cause returns the underlying error.
func (err *constraintError) Cause() error { return err.err }
// IsConstraintError returns true if the error is a constraint error.
func IsConstraintError(err error) bool {
_, ok := err.(*constraintError)
return ok
}
// Error implements the error interface.
func (err *constraintError) Error() string {
return fmt.Sprintf("violates constraint %q: %v", err.constraint, err.err)
}
// WithTx wraps DB code in a transaction.
func (db *DB) WithTx(ctx context.Context, fn func(context.Context, *Tx) error) (err error) {
return txutil.WithTx(ctx, db, nil, func(ctx context.Context, tx tagsql.Tx) error {
return fn(ctx, &Tx{
Tx: tx,
txMethods: db.wrapTx(tx),
})
})
}