forked from tolsen/mongonet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_error.go
69 lines (57 loc) · 1.18 KB
/
mongo_error.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
package mongonet
import (
"fmt"
"go.mongodb.org/mongo-driver/bson"
)
type MongoError struct {
err error
code int
codeName string
labels []string
}
func NewMongoError(err error, code int, codeName string) MongoError {
return MongoError{err, code, codeName, nil}
}
func NewMongoErrorWithLabels(err error, code int, codeName string, labels []string) MongoError {
return MongoError{err, code, codeName, labels}
}
func (me MongoError) HasLabel(label string) bool {
for _, val := range me.labels {
if val == label {
return true
}
}
return false
}
func (me MongoError) ToBSON() bson.D {
doc := bson.D{{"ok", 0}}
if me.err != nil {
doc = append(doc, bson.E{"errmsg", me.err.Error()})
}
doc = append(doc,
bson.E{"code", me.code},
bson.E{"codeName", me.codeName})
return doc
}
func (me MongoError) GetCode() int {
return me.code
}
func (me MongoError) GetCodeName() string {
return me.codeName
}
func (me MongoError) Error() string {
if me.err != nil {
return fmt.Sprintf(
"code=%v codeName=%v errmsg = %v",
me.code,
me.codeName,
me.err.Error(),
)
} else {
return fmt.Sprintf(
"code=%v codeName=%v",
me.code,
me.codeName,
)
}
}