From 04202c630328ed3b15132725ac77f5358c952dbb Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 9 Jan 2026 13:30:07 -0800 Subject: [PATCH] Refactor: Use %w for error wrapping and errors.As for extraction --- weed/filer/abstract_sql/abstract_sql_store.go | 28 +++++++++---------- weed/filer/mysql/mysql_store.go | 4 ++- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/weed/filer/abstract_sql/abstract_sql_store.go b/weed/filer/abstract_sql/abstract_sql_store.go index 2b768fbaf..cd44aad34 100644 --- a/weed/filer/abstract_sql/abstract_sql_store.go +++ b/weed/filer/abstract_sql/abstract_sql_store.go @@ -157,13 +157,13 @@ func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer.Ent doInsert = func() error { db, bucket, shortPath, err := store.getTxOrDB(ctx, entry.FullPath, false) if err != nil { - return fmt.Errorf("findDB %s : %v", entry.FullPath, err) + return fmt.Errorf("findDB %s : %w", entry.FullPath, err) } dir, name := shortPath.DirAndName() meta, err := entry.EncodeAttributesAndChunks() if err != nil { - return fmt.Errorf("encode %s: %s", entry.FullPath, err) + return fmt.Errorf("encode %s: %w", entry.FullPath, err) } if len(entry.GetChunks()) > filer.CountEntryChunksForGzip { @@ -178,12 +178,12 @@ func (store *AbstractSqlStore) InsertEntry(ctx context.Context, entry *filer.Ent res, err = db.ExecContext(ctx, store.GetSqlUpdate(bucket), meta, util.HashStringToLong(dir), name, dir) } if err != nil { - return fmt.Errorf("%s %s: %s", sqlInsert, entry.FullPath, err) + return fmt.Errorf("%s %s: %w", sqlInsert, entry.FullPath, err) } _, err = res.RowsAffected() if err != nil { - return fmt.Errorf("%s %s but no rows affected: %s", sqlInsert, entry.FullPath, err) + return fmt.Errorf("%s %s but no rows affected: %w", sqlInsert, entry.FullPath, err) } return nil @@ -201,23 +201,23 @@ func (store *AbstractSqlStore) UpdateEntry(ctx context.Context, entry *filer.Ent doUpdate = func() error { db, bucket, shortPath, err := store.getTxOrDB(ctx, entry.FullPath, false) if err != nil { - return fmt.Errorf("findDB %s : %v", entry.FullPath, err) + return fmt.Errorf("findDB %s : %w", entry.FullPath, err) } dir, name := shortPath.DirAndName() meta, err := entry.EncodeAttributesAndChunks() if err != nil { - return fmt.Errorf("encode %s: %s", entry.FullPath, err) + return fmt.Errorf("encode %s: %w", entry.FullPath, err) } res, err := db.ExecContext(ctx, store.GetSqlUpdate(bucket), meta, util.HashStringToLong(dir), name, dir) if err != nil { - return fmt.Errorf("update %s: %s", entry.FullPath, err) + return fmt.Errorf("update %s: %w", entry.FullPath, err) } _, err = res.RowsAffected() if err != nil { - return fmt.Errorf("update %s but no rows affected: %s", entry.FullPath, err) + return fmt.Errorf("update %s but no rows affected: %w", entry.FullPath, err) } return nil } @@ -262,19 +262,19 @@ func (store *AbstractSqlStore) DeleteEntry(ctx context.Context, fullpath util.Fu doDelete = func() error { db, bucket, shortPath, err := store.getTxOrDB(ctx, fullpath, false) if err != nil { - return fmt.Errorf("findDB %s : %v", fullpath, err) + return fmt.Errorf("findDB %s : %w", fullpath, err) } dir, name := shortPath.DirAndName() res, err := db.ExecContext(ctx, store.GetSqlDelete(bucket), util.HashStringToLong(dir), name, dir) if err != nil { - return fmt.Errorf("delete %s: %s", fullpath, err) + return fmt.Errorf("delete %s: %w", fullpath, err) } _, err = res.RowsAffected() if err != nil { - return fmt.Errorf("delete %s but no rows affected: %s", fullpath, err) + return fmt.Errorf("delete %s but no rows affected: %w", fullpath, err) } return nil } @@ -291,7 +291,7 @@ func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpat doDeleteFolderChildren = func() error { db, bucket, shortPath, err := store.getTxOrDB(ctx, fullpath, true) if err != nil { - return fmt.Errorf("findDB %s : %v", fullpath, err) + return fmt.Errorf("findDB %s : %w", fullpath, err) } if isValidBucket(bucket) && shortPath == "/" { @@ -308,12 +308,12 @@ func (store *AbstractSqlStore) DeleteFolderChildren(ctx context.Context, fullpat glog.V(4).InfofCtx(ctx, "delete %s SQL %s %d", string(shortPath), store.GetSqlDeleteFolderChildren(bucket), util.HashStringToLong(string(shortPath))) res, err := db.ExecContext(ctx, store.GetSqlDeleteFolderChildren(bucket), util.HashStringToLong(string(shortPath)), string(shortPath)) if err != nil { - return fmt.Errorf("deleteFolderChildren %s: %s", fullpath, err) + return fmt.Errorf("deleteFolderChildren %s: %w", fullpath, err) } _, err = res.RowsAffected() if err != nil { - return fmt.Errorf("deleteFolderChildren %s but no rows affected: %s", fullpath, err) + return fmt.Errorf("deleteFolderChildren %s but no rows affected: %w", fullpath, err) } return nil } diff --git a/weed/filer/mysql/mysql_store.go b/weed/filer/mysql/mysql_store.go index 351de075f..a4f8a3ce2 100644 --- a/weed/filer/mysql/mysql_store.go +++ b/weed/filer/mysql/mysql_store.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "crypto/x509" "database/sql" + "errors" "fmt" "os" "strings" @@ -68,7 +69,8 @@ func (store *MysqlStore) initialize(dsn string, upsertQuery string, enableUpsert } store.RetryableErrorCallback = func(err error) bool { - if mysqlError, ok := err.(*mysql.MySQLError); ok { + var mysqlError *mysql.MySQLError + if errors.As(err, &mysqlError) { if mysqlError.Number == 1213 { // ER_LOCK_DEADLOCK return true }