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

Bug: NamedQueryContext does not return sql.ErrNoRows, causing incorrect error handling #72

Open
nnpvaan opened this issue Feb 26, 2025 · 0 comments · May be fixed by #73
Open

Bug: NamedQueryContext does not return sql.ErrNoRows, causing incorrect error handling #72

nnpvaan opened this issue Feb 26, 2025 · 0 comments · May be fixed by #73
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@nnpvaan
Copy link

nnpvaan commented Feb 26, 2025

What were you trying to achieve?

I was trying to handle the case where a query returns no results using sqlx.NamedQueryContext. The expectation was that if no rows exist, it would return sql.ErrNoRows, allowing proper error handling.

What are the expected results?

If no rows are returned, NamedQueryContext should allow proper detection of this case in the error handling logic.

What are the received results?

Currently, NamedQueryContext does not return sql.ErrNoRows. Instead, it returns a valid *sqlx.Rows object with nil error. This means the following check will never trigger:

if err != nil {
    if err == sql.ErrNoRows {
        return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, err)
    }
}

Instead, the correct way to check for no results is via row.Next():

if !row.Next() { // This will correctly detect no rows
    return bootstrap.Config{}, errors.Wrap(repoerr.ErrNotFound, row.Err())
}

Steps To Reproduce

  1. Execute a query with NamedQueryContext where no rows match.
  2. Observe that err == nil, but row.Next() returns false.
  3. The existing error check for sql.ErrNoRows never triggers, leading to incorrect error handling.

In what environment did you encounter the issue?

Go version: 1.23.4
Magistrala version/commit: main

Additional information you deem important

No response

@nnpvaan nnpvaan added bug Something isn't working help wanted Extra attention is needed labels Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant