From 0f8263de2ecefdf17ae1e6d4349374751fbdd8a2 Mon Sep 17 00:00:00 2001 From: Dale Hui Date: Tue, 19 Feb 2019 01:59:04 -0800 Subject: [PATCH] Remove unnecessary panics from core implementation --- database/testing/testing.go | 6 +++--- migrate.go | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/database/testing/testing.go b/database/testing/testing.go index 01474836c..6d561d48e 100644 --- a/database/testing/testing.go +++ b/database/testing/testing.go @@ -16,7 +16,7 @@ import ( // Test runs tests against database implementations. func Test(t *testing.T, d database.Driver, migration []byte) { if migration == nil { - panic("test must provide migration reader") + t.Fatal("test must provide migration reader") } TestNilVersion(t, d) // test first @@ -46,7 +46,7 @@ func TestLockAndUnlock(t *testing.T, d database.Driver) { case <-done: return case <-timeout: - panic(fmt.Sprintf("Timeout after 15 seconds. Looks like a deadlock in Lock/UnLock.\n%#v", d)) + t.Fatal(fmt.Sprintf("Timeout after 15 seconds. Looks like a deadlock in Lock/UnLock.\n%#v", d)) } } }() @@ -81,7 +81,7 @@ func TestLockAndUnlock(t *testing.T, d database.Driver) { func TestRun(t *testing.T, d database.Driver, migration io.Reader) { if migration == nil { - panic("migration can't be nil") + t.Fatal("migration can't be nil") } if err := d.Run(migration); err != nil { diff --git a/migrate.go b/migrate.go index 8c8adfb5e..fa19dfc3c 100644 --- a/migrate.go +++ b/migrate.go @@ -5,6 +5,7 @@ package migrate import ( + "errors" "fmt" "os" "sync" @@ -25,10 +26,11 @@ var DefaultPrefetchMigrations = uint(10) var DefaultLockTimeout = 15 * time.Second var ( - ErrNoChange = fmt.Errorf("no change") - ErrNilVersion = fmt.Errorf("no migration") - ErrLocked = fmt.Errorf("database locked") - ErrLockTimeout = fmt.Errorf("timeout: can't acquire database lock") + ErrNoChange = errors.New("no change") + ErrNilVersion = errors.New("no migration") + ErrInvalidVersion = errors.New("version must be >= -1") + ErrLocked = errors.New("database locked") + ErrLockTimeout = errors.New("timeout: can't acquire database lock") ) // ErrShortLimit is an error returned when not enough migrations @@ -357,7 +359,7 @@ func (m *Migrate) Run(migration ...*Migration) error { // It resets the dirty state to false. func (m *Migrate) Force(version int) error { if version < -1 { - panic("version must be >= -1") + return ErrInvalidVersion } if err := m.lock(); err != nil { @@ -722,7 +724,7 @@ func (m *Migrate) runMigrations(ret <-chan interface{}) error { } default: - panic("unknown type") + return fmt.Errorf("unknown type: %T with value: %+v", r, r) } } return nil