Skip to content

Commit

Permalink
Correctly return encid.ErrNotFound in place of sql.ErrNoRows. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobg committed Dec 24, 2023
1 parent 4e3b830 commit 14879c2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 1 addition & 2 deletions cmd/encid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"crypto/aes"
"database/sql"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -88,7 +87,7 @@ func (c maincmd) tryEnc(ctx context.Context, fifty bool, typ int, n int64, isRet
} else {
id, str, err = encid.Encode(ctx, c.ks, typ, n)
}
if errors.Is(err, sql.ErrNoRows) && !isRetry {
if errors.Is(err, encid.ErrNotFound) && !isRetry {
if _, err = c.newKeyHelper(ctx, typ); err != nil {
return errors.Wrap(err, "creating new key")
}
Expand Down
12 changes: 10 additions & 2 deletions sqlite/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ func (ks *KeyStore) DecoderByID(ctx context.Context, id int64) (typ int, dec fun

var k []byte

if err := ks.db.QueryRowContext(ctx, q, id).Scan(&typ, &k); err != nil {
err = ks.db.QueryRowContext(ctx, q, id).Scan(&typ, &k)
if errors.Is(err, sql.ErrNoRows) {
return 0, nil, encid.ErrNotFound
}
if err != nil {
return 0, nil, errors.Wrapf(err, "retrieving key %d", id)
}

Expand All @@ -75,7 +79,11 @@ func (ks *KeyStore) EncoderByType(ctx context.Context, typ int) (id int64, enc f

var k []byte

if err := ks.db.QueryRowContext(ctx, q, typ).Scan(&id, &k); err != nil {
err = ks.db.QueryRowContext(ctx, q, typ).Scan(&id, &k)
if errors.Is(err, sql.ErrNoRows) {
return 0, nil, encid.ErrNotFound
}
if err != nil {
return 0, nil, errors.Wrapf(err, "retrieving key for type %d", typ)
}

Expand Down
10 changes: 5 additions & 5 deletions sqlite/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"crypto/aes"
"crypto/cipher"
"database/sql"
"errors"
"os"
"path/filepath"
"testing"

"github.com/bobg/encid"
"github.com/bobg/encid/testutil"
)

Expand All @@ -31,13 +31,13 @@ func TestKeyStore(t *testing.T) {
}

_, _, err = ks.DecoderByID(ctx, 1)
if !errors.Is(err, sql.ErrNoRows) {
t.Errorf("got %v, want %v", err, sql.ErrNoRows)
if !errors.Is(err, encid.ErrNotFound) {
t.Errorf("got %v, want %v", err, encid.ErrNotFound)
}

_, _, err = ks.EncoderByType(ctx, 1)
if !errors.Is(err, sql.ErrNoRows) {
t.Errorf("got %v, want %v", err, sql.ErrNoRows)
if !errors.Is(err, encid.ErrNotFound) {
t.Errorf("got %v, want %v", err, encid.ErrNotFound)
}

id, err := ks.NewKey(ctx, 1, aes.BlockSize)
Expand Down

0 comments on commit 14879c2

Please sign in to comment.