All checks were successful
check / check (push) Successful in 2m17s
Use errors.As with *sqlite.Error and SQLITE_CONSTRAINT_UNIQUE code instead of fragile strings.Contains(err.Error(), "UNIQUE") checks. Add db.IsUniqueConstraintError helper in internal/db/errors.go and replace all three string-matching call sites in api.go and auth.go.
21 lines
439 B
Go
21 lines
439 B
Go
// Package db provides database access and migration management.
|
|
package db
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"modernc.org/sqlite"
|
|
sqlite3 "modernc.org/sqlite/lib"
|
|
)
|
|
|
|
// IsUniqueConstraintError reports whether err is a SQLite
|
|
// unique-constraint violation.
|
|
func IsUniqueConstraintError(err error) bool {
|
|
var sqliteErr *sqlite.Error
|
|
if !errors.As(err, &sqliteErr) {
|
|
return false
|
|
}
|
|
|
|
return sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE
|
|
}
|