The length check and bcrypt call were copied into sign-up, reset, change and both admin paths; the unique-violation test into three api handlers and the library. checkNewPassword and hashPassword now sit in api/prelude.go, and db.IsUniqueViolation serves api and library. The taken-username and taken-email conflicts now carry a message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
18 lines
530 B
Go
18 lines
530 B
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/jackc/pgerrcode"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
// IsUniqueViolation reports whether err is Postgres refusing a row because
|
|
// another row already holds the same unique value: a username or email
|
|
// that is taken, or a second album row claiming one MBID. Callers use it to
|
|
// tell "that already exists" apart from a write that failed.
|
|
func IsUniqueViolation(err error) bool {
|
|
var pgErr *pgconn.PgError
|
|
return errors.As(err, &pgErr) && pgErr.Code == pgerrcode.UniqueViolation
|
|
}
|