Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ type mongoCollection struct {
keyType reflect.Type
}

// interprets mongo db error and returns library parsable error codes
func interpretMongoError(err error) error {
if mongo.IsDuplicateKeyError(err) {
return errors.Wrap(errors.AlreadyExists, err.Error())
}
if err == mongo.ErrNoDocuments {
return errors.Wrap(errors.NotFound, err.Error())
}
return err
}

// Set KeyType for the collection, this is not mandatory
// while the key type will be used by the interface implementer
// mainly for Watch Callback for providing decoded key, if not
Expand Down Expand Up @@ -84,9 +95,8 @@ func (c *mongoCollection) InsertOne(ctx context.Context, key interface{}, data i

_, err = c.col.InsertOne(ctx, bd)
if err != nil {
// TODO(prabhjot) we may need to identify and differentiate
// Already Exist error here.
return err
// identify and differentiate Already Exist error here.
return interpretMongoError(err)
}
return nil
}
Expand All @@ -113,7 +123,7 @@ func (c *mongoCollection) UpdateOne(ctx context.Context, key interface{}, data i
opts)

if err != nil {
return err
return interpretMongoError(err)
}

// check there should be at least one entry in matched count
Expand All @@ -133,7 +143,7 @@ func (c *mongoCollection) FindOne(ctx context.Context, key interface{}, data int
// object passed by the caller
if err := resp.Decode(data); err != nil {
// TODO(prabhjot) might have to identify not found error
return err
return interpretMongoError(err)
}
return nil
}
Expand All @@ -146,7 +156,7 @@ func (c *mongoCollection) FindMany(ctx context.Context, filter interface{}, data
}
cursor, err := c.col.Find(ctx, filter)
if err != nil {
return err
return interpretMongoError(err)
}
if err = cursor.All(ctx, data); err != nil {
return err
Expand All @@ -160,7 +170,7 @@ func (c *mongoCollection) DeleteOne(ctx context.Context, key interface{}) error
if err != nil {
// TODO(prabhjot) we may need to identify and differentiate
// Not found error here
return err
return interpretMongoError(err)
}
if resp.DeletedCount == 0 {
return errors.Wrap(errors.NotFound, "No Document found")
Expand All @@ -174,7 +184,7 @@ func (c *mongoCollection) DeleteOne(ctx context.Context, key interface{}) error
func (c *mongoCollection) DeleteMany(ctx context.Context, filter interface{}) (int64, error) {
resp, err := c.col.DeleteMany(ctx, filter)
if err != nil {
return 0, err
return 0, interpretMongoError(err)
}
if resp.DeletedCount == 0 {
return 0, errors.Wrap(errors.NotFound, "No matching entries found to delete")
Expand Down