Skip to content

Commit b2881fb

Browse files
committed
refactor(mongo): change error validation when no result is found
1 parent b06f57f commit b2881fb

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

  • pkg/context
    • shared/infrastructure/persistences/mongodb
    • user/infrastructure/persistence/mongodb/collection

pkg/context/shared/infrastructure/persistences/mongodb/mongodb.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/bastean/codexgo/v4/pkg/context/shared/domain/errors"
1515
)
1616

17+
const (
18+
ErrNoDocuments = "no documents"
19+
)
20+
1721
type Database struct {
1822
*mongo.Client
1923
*mongo.Database
@@ -89,7 +93,7 @@ func HandleErrDuplicateValue(err error) error {
8993
}
9094

9195
func IsErrNotFound(err error) bool {
92-
return err != nil
96+
return err != nil && strings.Contains(err.Error(), ErrNoDocuments)
9397
}
9498

9599
func HandleErrNotFound(err error, index string) error {

pkg/context/user/infrastructure/persistence/mongodb/collection/collection.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,18 @@ func (c *Collection) Search(criteria *repository.Criteria) (*user.User, error) {
132132

133133
err := result.Err()
134134

135-
if mongodb.IsErrNotFound(err) {
135+
switch {
136+
case mongodb.IsErrNotFound(err):
136137
return nil, mongodb.HandleErrNotFound(err, index)
138+
case err != nil:
139+
return nil, errors.New[errors.Internal](&errors.Bubble{
140+
Where: "Search",
141+
What: "Failure to find a User",
142+
Why: errors.Meta{
143+
"Index": index,
144+
},
145+
Who: err,
146+
})
137147
}
138148

139149
primitive := new(user.Primitive)

0 commit comments

Comments
 (0)