Skip to content

Commit

Permalink
Remove unnecessary panics from core implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhui committed Feb 19, 2019
1 parent 9b449be commit 0f8263d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions database/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
}()
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package migrate

import (
"errors"
"fmt"
"os"
"sync"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0f8263d

Please sign in to comment.