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
This commit is contained in:
Quinn Slack 2024-07-05 09:26:19 -05:00 committed by GitHub
parent fc011a19d2
commit f6fe8df922
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 1 deletions

View File

@ -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",

View File

@ -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 }

View File

@ -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()
}
}

View File

@ -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())}