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

Use sql.ErrNoRows as value for pgx.ErrNoRows #2115

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgx
import (
"context"
"crypto/sha256"
"database/sql"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -102,11 +103,27 @@ func (ident Identifier) Sanitize() string {

var (
// ErrNoRows occurs when rows are expected but none are returned.
ErrNoRows = errors.New("no rows in result set")
ErrNoRows = newProxyErr(sql.ErrNoRows, "no rows in result set")
// ErrTooManyRows occurs when more rows than expected are returned.
ErrTooManyRows = errors.New("too many rows in result set")
)

func newProxyErr(background error, msg string) error {
return &proxyError{
msg: msg,
background: background,
}
}

type proxyError struct {
msg string
background error
}

func (err *proxyError) Error() string { return err.msg }

func (err *proxyError) Unwrap() error { return err.background }

var (
errDisabledStatementCache = fmt.Errorf("cannot use QueryExecModeCacheStatement with disabled statement cache")
errDisabledDescriptionCache = fmt.Errorf("cannot use QueryExecModeCacheDescribe with disabled description cache")
Expand Down
11 changes: 11 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgx_test
import (
"bytes"
"context"
"database/sql"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -1408,3 +1409,13 @@ func TestConnDeallocateInvalidatedCachedStatementsInTransactionWithBatch(t *test

ensureConnValid(t, conn)
}

func TestErrNoRows(t *testing.T) {
t.Parallel()

// ensure we preserve old error message
require.Equal(t, "no rows in result set", pgx.ErrNoRows.Error())

require.ErrorIs(t, pgx.ErrNoRows, sql.ErrNoRows, "pgx.ErrNowRows must match sql.ErrNoRows")
require.ErrorIs(t, pgx.ErrNoRows, pgx.ErrNoRows, "sql.ErrNowRows must match pgx.ErrNoRows")
}