all: Make all error implementations pointer types (#6726)

This matches the convention of the stdlib and avoids ambiguity: when
customErr{} and &customErr{} both implement error, client code needs to
check for both.

Memory use should remain the same, since storing a non-pointer type in
an interface value still copies the value to the heap.
This commit is contained in:
greatroar
2020-06-16 09:27:34 +02:00
committed by GitHub
parent a47546a1f1
commit df83b84aa1
12 changed files with 36 additions and 48 deletions
+7 -17
View File
@@ -146,30 +146,20 @@ func OpenMemory() Backend {
type errClosed struct{}
func (errClosed) Error() string { return "database is closed" }
func (*errClosed) Error() string { return "database is closed" }
type errNotFound struct{}
func (errNotFound) Error() string { return "key not found" }
func (*errNotFound) Error() string { return "key not found" }
func IsClosed(err error) bool {
if _, ok := err.(errClosed); ok {
return true
}
if _, ok := err.(*errClosed); ok {
return true
}
return false
_, ok := err.(*errClosed)
return ok
}
func IsNotFound(err error) bool {
if _, ok := err.(errNotFound); ok {
return true
}
if _, ok := err.(*errNotFound); ok {
return true
}
return false
_, ok := err.(*errNotFound)
return ok
}
// releaser manages counting on top of a waitgroup
@@ -209,7 +199,7 @@ func (cg *closeWaitGroup) Add(i int) error {
cg.closeMut.RLock()
defer cg.closeMut.RUnlock()
if cg.closed {
return errClosed{}
return &errClosed{}
}
cg.WaitGroup.Add(i)
return nil
+2 -2
View File
@@ -402,10 +402,10 @@ func wrapBadgerErr(err error) error {
return nil
}
if err == badger.ErrDiscardedTxn {
return errClosed{}
return &errClosed{}
}
if err == badger.ErrKeyNotFound {
return errNotFound{}
return &errNotFound{}
}
return err
}
+2 -5
View File
@@ -207,14 +207,11 @@ func (it *leveldbIterator) Error() error {
// wrapLeveldbErr wraps errors so that the backend package can recognize them
func wrapLeveldbErr(err error) error {
if err == nil {
return nil
}
if err == leveldb.ErrClosed {
return errClosed{}
return &errClosed{}
}
if err == leveldb.ErrNotFound {
return errNotFound{}
return &errNotFound{}
}
return err
}
+3 -3
View File
@@ -149,12 +149,12 @@ func open(location string, opts *opt.Options) (*leveldb.DB, error) {
// the database and reindexing...
l.Infoln("Database corruption detected, unable to recover. Reinitializing...")
if err := os.RemoveAll(location); err != nil {
return nil, errorSuggestion{err, "failed to delete corrupted database"}
return nil, &errorSuggestion{err, "failed to delete corrupted database"}
}
db, err = leveldb.OpenFile(location, opts)
}
if err != nil {
return nil, errorSuggestion{err, "is another instance of Syncthing running?"}
return nil, &errorSuggestion{err, "is another instance of Syncthing running?"}
}
if debugEnvValue("CompactEverything", 0) != 0 {
@@ -227,6 +227,6 @@ type errorSuggestion struct {
suggestion string
}
func (e errorSuggestion) Error() string {
func (e *errorSuggestion) Error() string {
return fmt.Sprintf("%s (%s)", e.inner.Error(), e.suggestion)
}
+1 -1
View File
@@ -477,7 +477,7 @@ func TestDowngrade(t *testing.T) {
// Pretend we just opened the DB and attempt to update it again
err := UpdateSchema(db)
if err, ok := err.(databaseDowngradeError); !ok {
if err, ok := err.(*databaseDowngradeError); !ok {
t.Fatal("Expected error due to database downgrade, got", err)
} else if err.minSyncthingVersion != dbMinSyncthingVersion {
t.Fatalf("Error has %v as min Syncthing version, expected %v", err.minSyncthingVersion, dbMinSyncthingVersion)
+2 -2
View File
@@ -38,7 +38,7 @@ type databaseDowngradeError struct {
minSyncthingVersion string
}
func (e databaseDowngradeError) Error() string {
func (e *databaseDowngradeError) Error() string {
if e.minSyncthingVersion == "" {
return "newer Syncthing required"
}
@@ -67,7 +67,7 @@ func (db *schemaUpdater) updateSchema() error {
}
if prevVersion > dbVersion {
err := databaseDowngradeError{}
err := &databaseDowngradeError{}
if minSyncthingVersion, ok, dbErr := miscDB.String("dbMinSyncthingVersion"); dbErr != nil {
return dbErr
} else if ok {