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

feat(spanner): add support of checking row not found errors from ReadRow and ReadRowUsingIndex #10405

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions spanner/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package spanner

import (
"context"
"errors"
"fmt"

"github.com/googleapis/gax-go/v2/apierror"
Expand All @@ -26,6 +27,11 @@ import (
"google.golang.org/grpc/status"
)

var (
olavloite marked this conversation as resolved.
Show resolved Hide resolved
// ErrRecordNotFound record not found error
ErrRecordNotFound = errors.New("record not found")
Copy link
Contributor

Choose a reason for hiding this comment

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

ErrRowNotFound corresponds better with the original error message row not found...

Suggested change
ErrRecordNotFound = errors.New("record not found")
ErrRowNotFound = errors.New("record not found")

)

// Error is the structured error returned by Cloud Spanner client.
//
// Deprecated: Unwrap any error that is returned by the Spanner client as an APIError
Expand Down
6 changes: 6 additions & 0 deletions spanner/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,9 @@ func TestIntegration_Reads(t *testing.T) {
if ErrCode(err) != codes.NotFound {
t.Fatalf("got %v, want NotFound", err)
}
if !errors.Is(err, ErrRecordNotFound) {
t.Fatalf("got %v, want spanner.ErrRecordNotFound", err)
}
verifyDirectPathRemoteAddress(t)

// Index point read.
Expand All @@ -1751,6 +1754,9 @@ func TestIntegration_Reads(t *testing.T) {
if ErrCode(err) != codes.NotFound {
t.Fatalf("got %v, want NotFound", err)
}
if !errors.Is(err, ErrRecordNotFound) {
t.Fatalf("got %v, want spanner.ErrRecordNotFound", err)
}
verifyDirectPathRemoteAddress(t)
rangeReads(ctx, t, client)
indexRangeReads(ctx, t, client)
Expand Down
12 changes: 8 additions & 4 deletions spanner/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,16 @@ func (t *txReadOnly) ReadWithOptions(ctx context.Context, table string, keys Key
// errRowNotFound returns error for not being able to read the row identified by
// key.
func errRowNotFound(table string, key Key) error {
return spannerErrorf(codes.NotFound, "row not found(Table: %v, PrimaryKey: %v)", table, key)
err := spannerErrorf(codes.NotFound, "row not found(Table: %v, PrimaryKey: %v)", table, key)
err.(*Error).err = ErrRecordNotFound
return err
}

// errRowNotFoundByIndex returns error for not being able to read the row by index.
func errRowNotFoundByIndex(table string, key Key, index string) error {
return spannerErrorf(codes.NotFound, "row not found(Table: %v, IndexKey: %v, Index: %v)", table, key, index)
err := spannerErrorf(codes.NotFound, "row not found(Table: %v, IndexKey: %v, Index: %v)", table, key, index)
err.(*Error).err = ErrRecordNotFound
return err
}

// errMultipleRowsFound returns error for receiving more than one row when reading a single row using an index.
Expand All @@ -346,7 +350,7 @@ func errInlineBeginTransactionFailed() error {

// ReadRow reads a single row from the database.
//
// If no row is present with the given key, then ReadRow returns an error where
// If no row is present with the given key, then ReadRow returns an error(spanner.ErrRecordNotFound) where
// spanner.ErrCode(err) is codes.NotFound.
func (t *txReadOnly) ReadRow(ctx context.Context, table string, key Key, columns []string) (*Row, error) {
olavloite marked this conversation as resolved.
Show resolved Hide resolved
return t.ReadRowWithOptions(ctx, table, key, columns, nil)
Expand All @@ -373,7 +377,7 @@ func (t *txReadOnly) ReadRowWithOptions(ctx context.Context, table string, key K
// ReadRowUsingIndex reads a single row from the database using an index.
//
// If no row is present with the given index, then ReadRowUsingIndex returns an
// error where spanner.ErrCode(err) is codes.NotFound.
// error(spanner.ErrRecordNotFound) where spanner.ErrCode(err) is codes.NotFound.
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here: Maybe add an example in the comments for how users should check for this specific error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

//
// If more than one row received with the given index, then ReadRowUsingIndex
// returns an error where spanner.ErrCode(err) is codes.FailedPrecondition.
Expand Down
Loading