Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return false if nil is passed to IsNotExistError #2616

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions util/dbutil/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package dbutil
import (
"errors"
"fmt"
"os"
"io/fs"
"regexp"

"github.com/cockroachdb/pebble"
Expand All @@ -22,13 +22,15 @@ func IsErrNotFound(err error) bool {
var pebbleNotExistErrorRegex = regexp.MustCompile("pebble: database .* does not exist")

func isPebbleNotExistError(err error) bool {
return pebbleNotExistErrorRegex.MatchString(err.Error())
return err != nil && pebbleNotExistErrorRegex.MatchString(err.Error())
}

func isLeveldbNotExistError(err error) bool {
return os.IsNotExist(err)
return errors.Is(err, fs.ErrNotExist)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from the os.IsNotExist docs:

// IsNotExist returns a boolean indicating whether its argument is known to
// report that a file or directory does not exist. It is satisfied by
// [ErrNotExist] as well as some syscall errors.
//
// This function predates [errors.Is]. It only supports errors returned by
// the os package. New code should use errors.Is(err, fs.ErrNotExist).

}

// IsNotExistError returns true if the error is a "database not found" error.
// It must return false if err is nil.
func IsNotExistError(err error) bool {
return isLeveldbNotExistError(err) || isPebbleNotExistError(err)
}
Expand Down
3 changes: 3 additions & 0 deletions util/dbutil/dbutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func testIsNotExistError(t *testing.T, dbEngine string, isNotExist func(error) b
if isNotExist(err) {
t.Fatalf("Classified other error as not exist, err: %v", err)
}
if isNotExist(nil) {
t.Fatal("Classified nil as not exist")
}
}

func TestIsNotExistError(t *testing.T) {
Expand Down
Loading