mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-23 00:34:27 +01:00
use errors.Is
to determine what the error is
This commit is contained in:
parent
83ad8c7b1f
commit
1589f77956
@ -29,6 +29,7 @@ import (
|
|||||||
"github.com/araddon/dateparse"
|
"github.com/araddon/dateparse"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/mattn/go-sqlite3"
|
||||||
"github.com/rodaine/table"
|
"github.com/rodaine/table"
|
||||||
|
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
@ -708,13 +709,16 @@ func ReliableDbCreate(db *gorm.DB, entry interface{}) error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
errMsg := err.Error()
|
//errMsg := err.Error()
|
||||||
if errMsg == "database is locked (5) (SQLITE_BUSY)" || errMsg == "database is locked (261)" {
|
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)
|
time.Sleep(time.Duration(i*rand.Intn(100)) * time.Millisecond)
|
||||||
continue
|
continue
|
||||||
}
|
} else if errors.Is(err, sqlite3.ErrConstraintUnique) {
|
||||||
if strings.Contains(errMsg, "UNIQUE constraint failed") {
|
//if strings.Contains(errMsg, "UNIQUE constraint failed") {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
@ -723,10 +727,6 @@ func ReliableDbCreate(db *gorm.DB, entry interface{}) error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("unrecoverable sqlite error: %w", err)
|
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("failed to create DB entry even with %d retries: %w", i, err)
|
return fmt.Errorf("failed to create DB entry even with %d retries: %w", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user