@@ -38,7 +38,7 @@ func (db *DB) Open(uri string, opt ...database.Option) error {
3838
3939 clientOptions := options .Client ().ApplyURI (uri )
4040 if rs := clientOptions .ReplicaSet ; * rs == "" {
41- return fmt .Errorf ("to enable transactions, please provide a replica set name " )
41+ return fmt .Errorf ("replica set name is required to enable transactions " )
4242 }
4343
4444 ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
@@ -50,7 +50,7 @@ func (db *DB) Open(uri string, opt ...database.Option) error {
5050 }
5151
5252 if err = client .Ping (context .Background (), nil ); err != nil {
53- return fmt .Errorf ("failed connecting mongo to : %w" , err )
53+ return fmt .Errorf ("failed connecting to MongoDB : %w" , err )
5454 }
5555
5656 dbOpts := options .Database ()
@@ -67,10 +67,13 @@ func (db *DB) Close() error {
6767 return nil
6868}
6969
70+ // CreateTable creates a collection or an embedded collection if it does not exists.
7071func (db * DB ) CreateTable (collection []byte ) error {
7172 return db .createTable (context .Background (), collection )
7273}
7374
75+ // DeleteTable deletes a root or embedded collection. Returns an error if the
76+ // collection cannot be found or if the key represents a non-collection value.
7477func (db * DB ) DeleteTable (collection []byte ) error {
7578 return db .deleteTable (context .Background (), collection )
7679}
@@ -102,7 +105,7 @@ func (db *DB) CmpAndSwap(collection, key, oldValue, newValue []byte) ([]byte, bo
102105
103106 session , err := db .db .Client ().StartSession ()
104107 if err != nil {
105- return oldValue , false , fmt .Errorf ("failed starting session to : %w" , err )
108+ return oldValue , false , fmt .Errorf ("failed starting session: %w" , err )
106109 }
107110 defer session .EndSession (context .Background ())
108111
@@ -117,11 +120,11 @@ func (db *DB) CmpAndSwap(collection, key, oldValue, newValue []byte) ([]byte, bo
117120 if err = session .AbortTransaction (ctx ); err != nil {
118121 return fmt .Errorf ("failed to execute CmpAndSwap transaction on %s/%s and failed to rollback transaction: %w" , collection , key , err )
119122 }
120- return fmt .Errorf ("failed aborting transaction to : %w" , err )
123+ return fmt .Errorf ("failed aborting transaction: %w" , err )
121124 }
122125
123126 if err = session .CommitTransaction (ctx ); err != nil {
124- return fmt .Errorf ("failed committing transaction to : %w" , err )
127+ return fmt .Errorf ("failed committing transaction: %w" , err )
125128 }
126129 return nil
127130 })
@@ -136,7 +139,7 @@ func (db *DB) Update(tx *database.Tx) error {
136139
137140 session , err := db .db .Client ().StartSession ()
138141 if err != nil {
139- return fmt .Errorf ("failed starting session to : %w" , err )
142+ return fmt .Errorf ("failed starting session: %w" , err )
140143 }
141144 defer session .EndSession (context .Background ())
142145
@@ -151,7 +154,7 @@ func (db *DB) Update(tx *database.Tx) error {
151154 }
152155
153156 if err = session .CommitTransaction (ctx ); err != nil {
154- return fmt .Errorf ("failed committing transaction to : %w" , err )
157+ return fmt .Errorf ("failed committing transaction: %w" , err )
155158 }
156159 return nil
157160 })
@@ -163,10 +166,9 @@ func (db *DB) Update(tx *database.Tx) error {
163166 return nil
164167}
165168
166- // CreateTable creates a collection or an embedded collection if it does not exists.
167169func (db * DB ) createTable (ctx context.Context , collection []byte ) error {
168170 if err := db .db .CreateCollection (ctx , string (collection )); err != nil {
169- return fmt .Errorf ("failed creating collection %s to : %w" , collection , err )
171+ return fmt .Errorf ("failed creating collection %q : %w" , collection , err )
170172 }
171173
172174 // create an index on the Key field
@@ -177,21 +179,19 @@ func (db *DB) createTable(ctx context.Context, collection []byte) error {
177179
178180 _ , err := db .db .Collection (string (collection )).Indexes ().CreateOne (ctx , index )
179181 if err != nil {
180- return fmt .Errorf ("failed creating collection %s to : %w" , collection , err )
182+ return fmt .Errorf ("failed creating collection %q : %w" , collection , err )
181183 }
182184
183185 return nil
184186}
185187
186- // DeleteTable deletes a root or embedded collection. Returns an error if the
187- // collection cannot be found or if the key represents a non-collection value.
188188func (db * DB ) deleteTable (ctx context.Context , collection []byte ) error {
189189 if ! collectionExists (ctx , db .db , string (collection )) {
190- return fmt .Errorf ("failed deleting collection %s to : %w " , collection , database .ErrNotFound )
190+ return fmt .Errorf ("failed deleting collection %q : %w " , collection , database .ErrNotFound )
191191 }
192192
193193 if err := db .db .Collection (string (collection )).Drop (ctx ); err != nil {
194- return fmt .Errorf ("failed dropping collection %s to : %w" , collection , err )
194+ return fmt .Errorf ("failed dropping collection %q : %w" , collection , err )
195195 }
196196 return nil
197197}
@@ -201,12 +201,12 @@ func (db *DB) get(ctx context.Context, collection, key []byte) (ret []byte, err
201201 res := db .db .Collection (string (collection )).FindOne (ctx , filter )
202202
203203 if err := res .Err (); err != nil {
204- return nil , fmt .Errorf ("failed finding %s/%s to : %w" , collection , key , database .ErrNotFound )
204+ return nil , fmt .Errorf ("failed finding %s/%s: %w" , collection , key , database .ErrNotFound )
205205 }
206206
207207 result := tuple {}
208208 if err := res .Decode (& result ); err != nil {
209- return nil , fmt .Errorf ("failed decoding value for %s/%s to : %w" , collection , key , err )
209+ return nil , fmt .Errorf ("failed decoding value for %s/%s: %w" , collection , key , err )
210210 }
211211
212212 return result .Value , nil
@@ -219,15 +219,15 @@ func (db *DB) set(ctx context.Context, collection, key, value []byte) error {
219219
220220 _ , err := db .db .Collection (string (collection )).UpdateOne (ctx , filter , update , opts )
221221 if err != nil {
222- return fmt .Errorf ("failed setting value %s/%s to : %w" , collection , key , err )
222+ return fmt .Errorf ("failed setting value %s/%s: %w" , collection , key , err )
223223 }
224224 return nil
225225}
226226
227227// List returns the full list of entries in a collection.
228228func (db * DB ) list (ctx context.Context , collection []byte ) ([]* database.Entry , error ) {
229229 if ! collectionExists (ctx , db .db , string (collection )) {
230- return nil , fmt .Errorf ("failed finding collection %s : %w" , collection , database .ErrNotFound )
230+ return nil , fmt .Errorf ("failed finding collection %q : %w" , collection , database .ErrNotFound )
231231 }
232232
233233 // match all
@@ -240,7 +240,7 @@ func (db *DB) list(ctx context.Context, collection []byte) ([]*database.Entry, e
240240 defer cursor .Close (ctx )
241241
242242 if err = cursor .Err (); err != nil {
243- return nil , fmt .Errorf ("failed listing values of %s to : %w" , collection , err )
243+ return nil , fmt .Errorf ("failed listing values of %q : %w" , collection , err )
244244 }
245245
246246 var entries []* database.Entry
@@ -249,7 +249,7 @@ func (db *DB) list(ctx context.Context, collection []byte) ([]*database.Entry, e
249249 t := tuple {}
250250
251251 if err := cursor .Decode (& t ); err != nil {
252- return nil , fmt .Errorf ("failed decoding value to : %w" , err )
252+ return nil , fmt .Errorf ("failed decoding value: %w" , err )
253253 }
254254
255255 entries = append (entries , & database.Entry {
@@ -260,7 +260,7 @@ func (db *DB) list(ctx context.Context, collection []byte) ([]*database.Entry, e
260260 }
261261
262262 if err = cursor .Err (); err != nil {
263- return nil , fmt .Errorf ("failed listing values of collection %s to : %w" , collection , err )
263+ return nil , fmt .Errorf ("failed listing values of collection %q : %w" , collection , err )
264264 }
265265
266266 return entries , nil
@@ -272,11 +272,11 @@ func (db *DB) del(ctx context.Context, collection, key []byte) error {
272272
273273 mongoRes , err := db .db .Collection (string (collection )).DeleteOne (ctx , filter )
274274 if err != nil {
275- return fmt .Errorf ("failed deleting %s/%s to : %w" , collection , key , err )
275+ return fmt .Errorf ("failed deleting %s/%s: %w" , collection , key , err )
276276 }
277277
278278 if mongoRes .DeletedCount == 0 {
279- return fmt .Errorf ("failed to delete: %s/%s to : %w" , collection , key , database .ErrNotFound )
279+ return fmt .Errorf ("failed to delete: %s/%s: %w" , collection , key , database .ErrNotFound )
280280 }
281281
282282 return nil
@@ -359,7 +359,7 @@ func createUpdate(value []byte) bson.D {
359359func abort (ctx context.Context , session mongo.Session , err error ) error {
360360 abortError := session .AbortTransaction (ctx )
361361 if abortError != nil {
362- return fmt .Errorf ("failed aborting transaction to : %w" , err )
362+ return fmt .Errorf ("failed aborting transaction due to %q : %w" , abortError , err )
363363 }
364- return fmt .Errorf ("failed update to : %w" , err )
364+ return fmt .Errorf ("failed executing transaction : %w" , err )
365365}
0 commit comments