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.
1 Answer
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)
}
errors.Isand co.