-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
123 lines (99 loc) · 3.07 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
package dal
import (
"errors"
"fmt"
"strings"
)
// ErrNotSupported - return this if db name does not support requested operation.
// (for example no support for transactions)
var ErrNotSupported = errors.New("not supported")
// ErrNotImplementedYet - return this if db name does not support requested operation yet.
var ErrNotImplementedYet = errors.New("not implemented yet")
// ErrNoMoreRecords indicates there is no more records
var ErrNoMoreRecords = errors.New("no more errors")
// ErrDuplicateUser indicates there is a duplicate user // TODO: move to strongo/app?
type ErrDuplicateUser struct {
// TODO: Should it be moved out of this package to strongo/app/user?
SearchCriteria string
DuplicateUserIDs []string
}
var NoError = errors.New("no error")
// Error implements error interface
func (err ErrDuplicateUser) Error() string {
return fmt.Sprintf("multiple users by given search criteria[%v]: %v", err.SearchCriteria, strings.Join(err.DuplicateUserIDs, ","))
}
var (
// ErrRecordNotFound is returned when a DB record is not found
ErrRecordNotFound = errors.New("record not found")
)
// IsNotFound check if underlying error is ErrRecordNotFound
func IsNotFound(err error) bool {
if err == nil {
return false
}
if errors.Is(err, ErrRecordNotFound) {
return true
}
return false
}
// ErrNotFoundByKey indicates error was not found by Value
type ErrNotFoundByKey interface {
Key() *Key
Cause() error
error
}
var _ ErrNotFoundByKey = (*errNotFoundByKey)(nil)
type errNotFoundByKey struct {
key *Key
cause error
}
func (e errNotFoundByKey) Key() *Key {
return e.key
}
func (e errNotFoundByKey) Cause() error {
if e.cause == nil {
return ErrRecordNotFound
}
return e.cause
}
func (e errNotFoundByKey) Unwrap() error {
return e.Cause()
}
func (e errNotFoundByKey) Error() string {
if errors.Is(e.cause, ErrRecordNotFound) {
return fmt.Sprintf("%v: by key=%v", e.cause, e.key)
}
return fmt.Sprintf("%v: not found by key=%v", e.Cause(), e.key)
}
// NewErrNotFoundByKey creates an error that indicates that entity was not found by Value
func NewErrNotFoundByKey(key *Key, cause error) error {
return errNotFoundByKey{key: key, cause: errNotFoundCause(cause)}
}
func errNotFoundCause(cause error) error {
if cause == nil || cause == ErrRecordNotFound {
return ErrRecordNotFound
}
return fmt.Errorf("%w: %v", ErrRecordNotFound, cause)
}
type rollbackError struct {
originalErr error
rollbackErr error
}
func (v rollbackError) Error() string {
if v.originalErr == nil {
return fmt.Sprintf("rollback failed: %v", v.rollbackErr)
}
return fmt.Sprintf("rollback failed: %v: original error: %v", v.rollbackErr, v.originalErr)
}
func (v rollbackError) OriginalError() error {
return v.originalErr
}
func (v rollbackError) RollbackError() error {
return v.rollbackErr
}
// NewRollbackError creates a rollback error
func NewRollbackError(rollbackErr, originalErr error) error {
return &rollbackError{originalErr: originalErr, rollbackErr: rollbackErr}
}
// ErrHookFailed indicates that error occurred during hook execution
var ErrHookFailed = errors.New("failed in dalgo hook")