0

Golang project: I'm using Squirrel to generate SQL queries, and pgx/stdlib as a Postgres driver. I'm trying to account for a pretty common error, ErrNoRows, based on one of my queries, which is an acceptable error that I want to handle gracefully. The issue is pgx returns the error having text "no rows in result set", whereas Squirrel returns "sql: no rows in result set", so comparing them fails. Same text, just Squirrel is prefixing the error. What is the idiomatic way to handle this? I feel like substringing or replacing the "sql: " out isn't the appropriate way to go.

2
  • You should not be comparing the errors' string values, instead you should be comparing the error values themselves, and in the case of wrapped errors you should be using errors.Is and co. Commented May 28, 2021 at 18:51
  • I will not address what you should be looking for. But if you use a tool, and that tool itself adds anything then your code must either accept that addition, handle it or you use another tool. In this case substringing out the undesirable element is an entirely appropriate solution. IMHO: Just accept it and move on. Commented May 28, 2021 at 19:01

1 Answer 1

2

It's not squirrel prefixing the error, that is what database/sql has for sql.ErrNoRows.

For some reason, pgx does ErrNoRows = errors.New("no rows in result set")

But, since it's exported, you could just do:

pgx.ErrNoRows = sql.ErrNoRows

when your application starts up. Or write a function

func IsErrNoRows(err error) bool {
    return errors.Is(err, sql.ErrNoRows) || errors.Is(err, pgx.ErrNoRows)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.