@@ -157,7 +157,9 @@ func NewWithDB(db *sql.DB, opts ...Option) (*SQLiteStorage, error) {
157157// If the storage was created with NewWithDB(), this is a no-op.
158158func (s * SQLiteStorage ) Close () error {
159159 if s .managedDB {
160- return s .db .Close ()
160+ if err := s .db .Close (); err != nil {
161+ return fmt .Errorf ("close database: %w" , err )
162+ }
161163 }
162164 return nil
163165}
@@ -178,8 +180,10 @@ func (s *SQLiteStorage) initSchema(db *sql.DB) error {
178180 ctx , cancel := context .WithTimeout (context .Background (), s .queryTimeout )
179181 defer cancel ()
180182
181- _ , err := db .ExecContext (ctx , schema )
182- return err
183+ if _ , err := db .ExecContext (ctx , schema ); err != nil {
184+ return fmt .Errorf ("exec schema: %w" , err )
185+ }
186+ return nil
183187}
184188
185189func normalizeKey (key string ) string {
@@ -204,14 +208,16 @@ func (s *SQLiteStorage) Store(ctx context.Context, key string, value []byte) err
204208 ctx , cancel := context .WithTimeout (ctx , s .queryTimeout )
205209 defer cancel ()
206210
207- _ , err := s .db .ExecContext (ctx , `
211+ if _ , err := s .db .ExecContext (ctx , `
208212 INSERT INTO certmagic_data(key, value, modified)
209213 VALUES(?, ?, ?)
210214 ON CONFLICT(key) DO UPDATE SET
211215 value=excluded.value,
212216 modified=excluded.modified` ,
213- key , value , time .Now ().UnixMilli ())
214- return err
217+ key , value , time .Now ().UnixMilli ()); err != nil {
218+ return fmt .Errorf ("store %q: %w" , key , err )
219+ }
220+ return nil
215221}
216222
217223// Load retrieves the value at the given key.
@@ -227,7 +233,10 @@ func (s *SQLiteStorage) Load(ctx context.Context, key string) ([]byte, error) {
227233 if errors .Is (err , sql .ErrNoRows ) {
228234 return nil , fs .ErrNotExist
229235 }
230- return value , err
236+ if err != nil {
237+ return nil , fmt .Errorf ("load %q: %w" , key , err )
238+ }
239+ return value , nil
231240}
232241
233242// Delete removes the key and all keys with the same prefix.
@@ -237,15 +246,19 @@ func (s *SQLiteStorage) Delete(ctx context.Context, key string) error {
237246 defer cancel ()
238247
239248 if key == "" {
240- _ , err := s .db .ExecContext (ctx , `DELETE FROM certmagic_data` )
241- return err
249+ if _ , err := s .db .ExecContext (ctx , `DELETE FROM certmagic_data` ); err != nil {
250+ return fmt .Errorf ("delete all: %w" , err )
251+ }
252+ return nil
242253 }
243254
244255 start , end := prefixRange (key + "/" )
245- _ , err := s .db .ExecContext (ctx ,
256+ if _ , err := s .db .ExecContext (ctx ,
246257 `DELETE FROM certmagic_data WHERE key = ? OR (key >= ? AND key < ?)` ,
247- key , start , end )
248- return err
258+ key , start , end ); err != nil {
259+ return fmt .Errorf ("delete %q: %w" , key , err )
260+ }
261+ return nil
249262}
250263
251264// Exists returns true if the key exists (either as a file or prefix).
@@ -280,7 +293,7 @@ func (s *SQLiteStorage) List(ctx context.Context, prefix string, recursive bool)
280293 `SELECT key FROM certmagic_data WHERE key >= ? AND key < ? ORDER BY key` ,
281294 start , end )
282295 if err != nil {
283- return nil , err
296+ return nil , fmt . Errorf ( "list %q: %w" , prefix , err )
284297 }
285298 defer func () { retErr = errors .Join (retErr , rows .Close ()) }()
286299
@@ -290,7 +303,7 @@ func (s *SQLiteStorage) List(ctx context.Context, prefix string, recursive bool)
290303 for rows .Next () {
291304 var key string
292305 if err := rows .Scan (& key ); err != nil {
293- return nil , err
306+ return nil , fmt . Errorf ( "list scan: %w" , err )
294307 }
295308
296309 if recursive {
@@ -308,7 +321,7 @@ func (s *SQLiteStorage) List(ctx context.Context, prefix string, recursive bool)
308321 }
309322
310323 if err := rows .Err (); err != nil {
311- return nil , err
324+ return nil , fmt . Errorf ( "list rows: %w" , err )
312325 }
313326 if len (results ) == 0 {
314327 return nil , fs .ErrNotExist
@@ -337,7 +350,7 @@ func (s *SQLiteStorage) Stat(ctx context.Context, key string) (certmagic.KeyInfo
337350 }, nil
338351 }
339352 if ! errors .Is (err , sql .ErrNoRows ) {
340- return certmagic.KeyInfo {}, err
353+ return certmagic.KeyInfo {}, fmt . Errorf ( "stat %q: %w" , key , err )
341354 }
342355
343356 start , end := prefixRange (key + "/" )
@@ -396,7 +409,7 @@ func (s *SQLiteStorage) Lock(ctx context.Context, name string) error {
396409
397410 for {
398411 if err := ctx .Err (); err != nil {
399- return err
412+ return fmt . Errorf ( "lock %q: %w" , name , err )
400413 }
401414
402415 acquired , err := s .tryLock (ctx , name )
@@ -412,7 +425,7 @@ func (s *SQLiteStorage) Lock(ctx context.Context, name string) error {
412425
413426 select {
414427 case <- ctx .Done ():
415- return ctx .Err ()
428+ return fmt . Errorf ( "lock %q: %w" , name , ctx .Err () )
416429 case <- time .After (pollInterval + lockPollJitter (50 )):
417430 }
418431 }
@@ -436,7 +449,7 @@ func (s *SQLiteStorage) tryLock(ctx context.Context, name string) (bool, error)
436449 if isBusyError (err ) {
437450 return false , nil
438451 }
439- return false , err
452+ return false , fmt . Errorf ( "trylock insert %q: %w" , name , err )
440453 }
441454
442455 result , err := s .db .ExecContext (ctx ,
@@ -447,7 +460,7 @@ func (s *SQLiteStorage) tryLock(ctx context.Context, name string) (bool, error)
447460 if isBusyError (err ) {
448461 return false , nil
449462 }
450- return false , err
463+ return false , fmt . Errorf ( "trylock update %q: %w" , name , err )
451464 }
452465 rows , _ := result .RowsAffected ()
453466 return rows > 0 , nil
@@ -467,7 +480,7 @@ func (s *SQLiteStorage) Unlock(ctx context.Context, name string) error {
467480 `DELETE FROM certmagic_locks WHERE name = ? AND owner_id = ?` ,
468481 name , s .ownerID )
469482 if err != nil {
470- return err
483+ return fmt . Errorf ( "unlock %q: %w" , name , err )
471484 }
472485
473486 rows , _ := result .RowsAffected ()
0 commit comments