lib/db: Make err(Closed|NotFound) values (#8215)

This commit is contained in:
greatroar 2022-03-13 20:53:34 +01:00 committed by GitHub
parent 6862dd04ab
commit d0fd6c6c82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 22 deletions

View File

@ -133,23 +133,13 @@ func OpenMemory() Backend {
return OpenLevelDBMemory() return OpenLevelDBMemory()
} }
type errClosed struct{} var (
errClosed = errors.New("database is closed")
errNotFound = errors.New("key not found")
)
func (*errClosed) Error() string { return "database is closed" } func IsClosed(err error) bool { return errors.Is(err, errClosed) }
func IsNotFound(err error) bool { return errors.Is(err, errNotFound) }
type errNotFound struct{}
func (*errNotFound) Error() string { return "key not found" }
func IsClosed(err error) bool {
e := &errClosed{}
return errors.As(err, &e)
}
func IsNotFound(err error) bool {
e := &errNotFound{}
return errors.As(err, &e)
}
// releaser manages counting on top of a waitgroup // releaser manages counting on top of a waitgroup
type releaser struct { type releaser struct {
@ -183,7 +173,7 @@ func (cg *closeWaitGroup) Add(i int) error {
cg.closeMut.RLock() cg.closeMut.RLock()
defer cg.closeMut.RUnlock() defer cg.closeMut.RUnlock()
if cg.closed { if cg.closed {
return &errClosed{} return errClosed
} }
cg.WaitGroup.Add(i) cg.WaitGroup.Add(i)
return nil return nil

View File

@ -223,11 +223,11 @@ func (it *leveldbIterator) Error() error {
// wrapLeveldbErr wraps errors so that the backend package can recognize them // wrapLeveldbErr wraps errors so that the backend package can recognize them
func wrapLeveldbErr(err error) error { func wrapLeveldbErr(err error) error {
if err == leveldb.ErrClosed { switch err {
return &errClosed{} case leveldb.ErrClosed:
} return errClosed
if err == leveldb.ErrNotFound { case leveldb.ErrNotFound:
return &errNotFound{} return errNotFound
} }
return err return err
} }