Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
add database.NotFoundError helper type (#63671)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sqs authored and Chickensoupwithrice committed Jul 10, 2024
1 parent 1ae58ee commit eca5706
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/database/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions internal/database/errors.go
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions internal/database/errors_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

}
2 changes: 1 addition & 1 deletion internal/database/search_contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())}
Expand Down

0 comments on commit eca5706

Please sign in to comment.