From b1fd2f1b963cd59316d35b5791bfb605875254f0 Mon Sep 17 00:00:00 2001 From: clawbot Date: Tue, 10 Mar 2026 11:54:27 +0100 Subject: [PATCH] Replace string-matching error detection with typed SQLite errors (closes #39) (#66) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Replaces fragile `strings.Contains(err.Error(), "UNIQUE")` checks with typed error detection using `errors.As` and the SQLite driver's `*sqlite.Error` type. ## Changes - **`internal/db/errors.go`** (new): Adds `IsUniqueConstraintError(err)` helper that uses `errors.As` to unwrap the error into `*sqlite.Error` and checks for `SQLITE_CONSTRAINT_UNIQUE` (code 2067). - **`internal/handlers/api.go`**: Replaces two `strings.Contains(err.Error(), "UNIQUE")` calls with `db.IsUniqueConstraintError(err)` — in `handleCreateSessionError` and `executeNickChange`. - **`internal/handlers/auth.go`**: Replaces one `strings.Contains(err.Error(), "UNIQUE")` call with `db.IsUniqueConstraintError(err)` — in `handleRegisterError`. ## Why String matching on error messages is fragile — if the SQLite driver changes its error message format, the detection silently breaks. Using `errors.As` with the driver's typed error and checking the specific SQLite error code is robust, idiomatic Go, and immune to message format changes. closes https://git.eeqj.de/sneak/chat/issues/39 Co-authored-by: user Co-authored-by: Jeffrey Paul Reviewed-on: https://git.eeqj.de/sneak/chat/pulls/66 Co-authored-by: clawbot Co-committed-by: clawbot --- internal/db/errors.go | 20 ++++++++++++++++++++ internal/handlers/api.go | 5 +++-- internal/handlers/auth.go | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 internal/db/errors.go diff --git a/internal/db/errors.go b/internal/db/errors.go new file mode 100644 index 0000000..7f5ad49 --- /dev/null +++ b/internal/db/errors.go @@ -0,0 +1,20 @@ +// 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 +} diff --git a/internal/handlers/api.go b/internal/handlers/api.go index b1c7c87..fce5101 100644 --- a/internal/handlers/api.go +++ b/internal/handlers/api.go @@ -10,6 +10,7 @@ import ( "strings" "time" + "git.eeqj.de/sneak/neoirc/internal/db" "git.eeqj.de/sneak/neoirc/internal/irc" "github.com/go-chi/chi" ) @@ -199,7 +200,7 @@ func (hdlr *Handlers) handleCreateSessionError( request *http.Request, err error, ) { - if strings.Contains(err.Error(), "UNIQUE") { + if db.IsUniqueConstraintError(err) { hdlr.respondError( writer, request, "nick already taken", @@ -1427,7 +1428,7 @@ func (hdlr *Handlers) executeNickChange( request.Context(), sessionID, newNick, ) if err != nil { - if strings.Contains(err.Error(), "UNIQUE") { + if db.IsUniqueConstraintError(err) { hdlr.respondIRCError( writer, request, clientID, sessionID, irc.ErrNicknameInUse, nick, []string{newNick}, diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index ceb320f..cd99bf7 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -4,6 +4,8 @@ import ( "encoding/json" "net/http" "strings" + + "git.eeqj.de/sneak/neoirc/internal/db" ) const minPasswordLength = 8 @@ -94,7 +96,7 @@ func (hdlr *Handlers) handleRegisterError( request *http.Request, err error, ) { - if strings.Contains(err.Error(), "UNIQUE") { + if db.IsUniqueConstraintError(err) { hdlr.respondError( writer, request, "nick already taken",