From 578113e07cb8b7e25e75b408234c0d21c2b77d4c Mon Sep 17 00:00:00 2001 From: David Dworken Date: Wed, 6 Sep 2023 20:21:00 -0700 Subject: [PATCH] Revert "use `errors.Is` to determine what the error is" since it doesn't work with modernc.org/sqlite which we need in order to avoid using CGO (which we do to enable easy cross-compiles) This reverts commit 1589f779566d3c85ef0eb568acf7a6347398463a. When building with CGO_ENABLED=0, this code will fail to build. --- client/lib/lib.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/client/lib/lib.go b/client/lib/lib.go index e2817b1..e698a8b 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -29,7 +29,6 @@ import ( "github.com/araddon/dateparse" "github.com/fatih/color" "github.com/google/uuid" - "github.com/mattn/go-sqlite3" "github.com/rodaine/table" "github.com/ddworken/hishtory/client/data" @@ -709,23 +708,24 @@ func ReliableDbCreate(db *gorm.DB, entry interface{}) error { if err == nil { return nil } - - //errMsg := err.Error() - if errors.Is(err, sqlite3.ErrBusy) || errors.Is(err, sqlite3.ErrLocked) { - // accounts for wrapped errors like: - // * "database is locked (5) (SQLITE_BUSY)" - // * "database is locked (261)" -- 261 is SQLITE_BUSY_RECOVERY (5 || 1<<8) - time.Sleep(time.Duration(i*rand.Intn(100)) * time.Millisecond) - continue - } else if errors.Is(err, sqlite3.ErrConstraintUnique) { - //if strings.Contains(errMsg, "UNIQUE constraint failed") { - if i == 0 { - return err - } else { - return nil + if err != nil { + errMsg := err.Error() + if errMsg == "database is locked (5) (SQLITE_BUSY)" || errMsg == "database is locked (261)" { + time.Sleep(time.Duration(i*rand.Intn(100)) * time.Millisecond) + continue } + if strings.Contains(errMsg, "UNIQUE constraint failed") { + if i == 0 { + return err + } else { + return nil + } + } + return fmt.Errorf("unrecoverable sqlite error: %w", err) + } + if err != nil && err.Error() != "database is locked (5) (SQLITE_BUSY)" { + return fmt.Errorf("unrecoverable sqlite error: %w", err) } - return fmt.Errorf("unrecoverable sqlite error: %w", err) } return fmt.Errorf("failed to create DB entry even with %d retries: %w", i, err) }