mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 15:51:43 +00:00
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:
parent
fc011a19d2
commit
f6fe8df922
@ -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",
|
||||
|
||||
19
internal/database/errors.go
Normal file
19
internal/database/errors.go
Normal 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 }
|
||||
22
internal/database/errors_test.go
Normal file
22
internal/database/errors_test.go
Normal 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()
|
||||
}
|
||||
|
||||
}
|
||||
@ -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())}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user