forked from go-rel/rel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
98 lines (81 loc) · 2.87 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
package rel
var (
// ErrNotFound returned when records not found.
ErrNotFound = NotFoundError{}
// ErrCheckConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrCheckConstraint).
ErrCheckConstraint = ConstraintError{Type: CheckConstraint}
// ErrNotNullConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrNotNullConstraint).
ErrNotNullConstraint = ConstraintError{Type: NotNullConstraint}
// ErrUniqueConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrUniqueConstraint).
ErrUniqueConstraint = ConstraintError{Type: UniqueConstraint}
// ErrPrimaryKeyConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrPrimaryKeyConstraint).
ErrPrimaryKeyConstraint = ConstraintError{Type: PrimaryKeyConstraint}
// ErrForeignKeyConstraint is an auxiliary variable for error handling.
// This is only to be used when checking error with errors.Is(err, ErrForeignKeyConstraint).
ErrForeignKeyConstraint = ConstraintError{Type: ForeignKeyConstraint}
)
// NotFoundError returned whenever Find returns no result.
type NotFoundError struct{}
// Error message.
func (nfe NotFoundError) Error() string {
return "Record not found"
}
// ConstraintType defines the type of constraint error.
type ConstraintType int8
const (
// CheckConstraint error type.
CheckConstraint ConstraintType = iota
// NotNullConstraint error type.1
NotNullConstraint
// UniqueConstraint error type.1
UniqueConstraint
// PrimaryKeyConstraint error type.1
PrimaryKeyConstraint
// ForeignKeyConstraint error type.1
ForeignKeyConstraint
)
// String representation of the constraint type.
func (ct ConstraintType) String() string {
switch ct {
case CheckConstraint:
return "CheckConstraint"
case NotNullConstraint:
return "NotNullConstraint"
case UniqueConstraint:
return "UniqueConstraint"
case PrimaryKeyConstraint:
return "PrimaryKeyConstraint"
case ForeignKeyConstraint:
return "ForeignKeyConstraint"
default:
return ""
}
}
// ConstraintError returned whenever constraint error encountered.
type ConstraintError struct {
Key string
Type ConstraintType
Err error
}
// Is returns true when target error have the same type and key if defined.
func (ce ConstraintError) Is(target error) bool {
if err, ok := target.(ConstraintError); ok {
return ce.Type == err.Type && (ce.Key == "" || err.Key == "" || ce.Key == err.Key)
}
return false
}
// Unwrap internal error returned by database driver.
func (ce ConstraintError) Unwrap() error {
return ce.Err
}
// Error message.
func (ce ConstraintError) Error() string {
if ce.Err != nil {
return ce.Type.String() + "Error: " + ce.Err.Error()
}
return ce.Type.String() + "Error"
}