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

Make IsErrNotFound more flexible #61

Merged
merged 2 commits into from
Apr 29, 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
3 changes: 2 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"database/sql"
"fmt"

"errors"
Expand All @@ -17,7 +18,7 @@ var (

// IsErrNotFound returns true if the cause of the given error is ErrNotFound.
func IsErrNotFound(err error) bool {
return errors.Is(err, ErrNotFound)
return errors.Is(err, ErrNotFound) || errors.Is(err, sql.ErrNoRows)
}

// IsErrOpNotSupported returns true if the cause of the given error is ErrOpNotSupported.
Expand Down
10 changes: 10 additions & 0 deletions nosql_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package nosql

import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -357,3 +359,11 @@ func TestBolt(t *testing.T) {

run(t, db)
}

func TestIsErrNotFound(t *testing.T) {
assert.True(t, IsErrNotFound(database.ErrNotFound))
assert.True(t, IsErrNotFound(sql.ErrNoRows))
assert.True(t, IsErrNotFound(fmt.Errorf("something failed: %w", database.ErrNotFound)))
assert.True(t, IsErrNotFound(fmt.Errorf("something failed: %w", sql.ErrNoRows)))
assert.False(t, IsErrNotFound(errors.New("not found")))
}
Loading