From f6fe8df922e3fdd4407e3bf1d78649d0a66ae56b Mon Sep 17 00:00:00 2001 From: Quinn Slack Date: Fri, 5 Jul 2024 09:26:19 -0500 Subject: [PATCH] add database.NotFoundError helper type (#63671) Use it in the 1 place it seemed obvious. I have other changes where it will be used in more places. This helps standardize our codebase. ## Test plan CI --- internal/database/BUILD.bazel | 2 ++ internal/database/errors.go | 19 +++++++++++++++++++ internal/database/errors_test.go | 22 ++++++++++++++++++++++ internal/database/search_contexts.go | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 internal/database/errors.go create mode 100644 internal/database/errors_test.go diff --git a/internal/database/BUILD.bazel b/internal/database/BUILD.bazel index 4bbb75e261e..906276eb422 100644 --- a/internal/database/BUILD.bazel +++ b/internal/database/BUILD.bazel @@ -28,6 +28,7 @@ go_library( "conf.go", "database.go", "doc.go", + "errors.go", "event_logs.go", "event_logs_scrape_state_own.go", "executor_secret_access_logs.go", @@ -200,6 +201,7 @@ go_test( "database_test.go", "dbstore_db_test.go", "err_test.go", + "errors_test.go", "event_logs_test.go", "executor_secret_access_logs_test.go", "executor_secrets_internal_test.go", diff --git a/internal/database/errors.go b/internal/database/errors.go new file mode 100644 index 00000000000..f6be7807bc8 --- /dev/null +++ b/internal/database/errors.go @@ -0,0 +1,19 @@ +package database + +// resourceNotFoundError is an error that indicates that a database resource was not found. It can be +// returned by methods that get a single resource (such as Get or GetByXyz). +// +// errcode.IsNotFound(err) == true for notFoundError values. +type resourceNotFoundError struct { + noun string +} + +func (e resourceNotFoundError) Error() string { + const notFound = "not found" + if e.noun == "" { + return notFound + } + return e.noun + " " + notFound +} + +func (resourceNotFoundError) NotFound() bool { return true } diff --git a/internal/database/errors_test.go b/internal/database/errors_test.go new file mode 100644 index 00000000000..99284884198 --- /dev/null +++ b/internal/database/errors_test.go @@ -0,0 +1,22 @@ +package database + +import ( + "testing" + + "github.com/sourcegraph/sourcegraph/internal/errcode" +) + +func TestResourceNotFoundError(t *testing.T) { + err := resourceNotFoundError{"foo"} + if want := "foo not found"; err.Error() != want { + t.Errorf("got %q, want %q", err, want) + } + + if !errcode.IsNotFound(resourceNotFoundError{"foo"}) { + t.Fatal() + } + if !errcode.IsNotFound(&resourceNotFoundError{"foo"}) { + t.Fatal() + } + +} diff --git a/internal/database/search_contexts.go b/internal/database/search_contexts.go index f674a9d3a50..5e3441e060c 100644 --- a/internal/database/search_contexts.go +++ b/internal/database/search_contexts.go @@ -19,7 +19,7 @@ import ( "github.com/sourcegraph/sourcegraph/lib/errors" ) -var ErrSearchContextNotFound = errors.New("search context not found") +var ErrSearchContextNotFound = resourceNotFoundError{noun: "search context"} func SearchContextsWith(logger log.Logger, other basestore.ShareableStore) SearchContextsStore { return &searchContextsStore{logger: logger, Store: basestore.NewWithHandle(other.Handle())}