/
check.go
92 lines (80 loc) · 1.66 KB
/
check.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
package errorType
import (
"context"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/mongo"
)
func IsNotFoundErr(err error) bool {
for err != nil {
switch err.(type) {
case *notFoundError:
return true
}
err = errors.Unwrap(err)
}
return false
}
func IsDuplicatedKeyErr(err error) bool {
for err != nil {
switch err.(type) {
case *duplicatedKeyError:
return true
}
err = errors.Unwrap(err)
}
return false
}
func IsTimeoutError(err error) bool {
for err != nil {
switch err.(type) {
case *timeoutError:
return true
}
err = errors.Unwrap(err)
}
return false
}
func IsMongoClientError(err error) bool {
for err != nil {
switch err.(type) {
case *mongoClientError:
return true
}
err = errors.Unwrap(err)
}
return false
}
func IsDBInternalErr(err error) bool {
for err != nil {
switch err.(type) {
case *internalError,
*timeoutError,
*mongoClientError:
return true
}
err = errors.Unwrap(err)
}
return false
}
func IsDecodeError(err error) bool {
for err != nil {
switch err.(type) {
case *decodeError:
return true
}
err = errors.Unwrap(err)
}
return false
}
func ParseAndReturnDBError(err error, collection string, filter, update, doc interface{}) error {
if errors.Is(err, mongo.ErrNoDocuments) || errors.Is(err, NotMatchedAnyErr) {
return NotFoundError(collection, filter, update, doc)
}
if mongo.IsDuplicateKeyError(err) {
return DuplicatedKeyError(collection, filter, update, doc, err)
}
if mongo.IsTimeout(err) || errors.Is(err, context.DeadlineExceeded) {
return TimeoutError(collection, filter, update, doc, err)
}
return InternalError(collection, filter, update, doc, err)
}