From d0346b23825b2a2bfbcd5949c36bb42eb42135c4 Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 29 Nov 2023 19:52:10 -0600 Subject: [PATCH] use proper errors for db.Err - switch to using errors.Is where applicable --- internal/db/account.go | 6 +++--- internal/db/db_test.go | 3 ++- login/login.go | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/db/account.go b/internal/db/account.go index 372d721..2b6b801 100644 --- a/internal/db/account.go +++ b/internal/db/account.go @@ -1,7 +1,7 @@ package db import ( - "fmt" + "errors" "log" "golang.org/x/crypto/bcrypt" @@ -44,8 +44,8 @@ func (db *DBHandler) NewAccount(Login, Password string) (*Account, error) { } var ( - ErrLoginInvalidID = fmt.Errorf("invalid Login ID") - ErrLoginInvalidPassword = fmt.Errorf("invalid ID && Password combo") + ErrLoginInvalidID = errors.New("invalid Login ID") + ErrLoginInvalidPassword = errors.New("invalid ID && Password combo") ) func (db *DBHandler) TryLogin(Login, Password string) (*Account, error) { diff --git a/internal/db/db_test.go b/internal/db/db_test.go index 54572ab..76b0a8c 100644 --- a/internal/db/db_test.go +++ b/internal/db/db_test.go @@ -2,6 +2,7 @@ package db_test import ( "context" + "errors" "os" "testing" @@ -50,7 +51,7 @@ func TestDBAccount(t *testing.T) { t.Error("account username is not test") } - if _, err = testDB.TryLogin("test", "wrongpassword"); err != db.ErrLoginInvalidPassword { + if _, err = testDB.TryLogin("test", "wrongpassword"); !errors.Is(err, db.ErrLoginInvalidPassword) { t.Error("expected ErrLoginInvalidPassword") } } diff --git a/login/login.go b/login/login.go index fb3b449..3337d6c 100644 --- a/login/login.go +++ b/login/login.go @@ -2,6 +2,7 @@ package login import ( "encoding/binary" + "errors" "fmt" "log" "math/rand" @@ -79,7 +80,7 @@ func (server *LoginServer) Login(peer *protocol.CNPeer, _account interface{}, pk // attempt login account, err := server.dbHndlr.TryLogin(loginPkt.SzID, loginPkt.SzPassword) - if err == db.ErrLoginInvalidID { + if errors.Is(err, db.ErrLoginInvalidID) { // this is the default behavior, auto create the account if the ID isn't in use account, err = server.dbHndlr.NewAccount(loginPkt.SzID, loginPkt.SzPassword) if err != nil { @@ -87,7 +88,7 @@ func (server *LoginServer) Login(peer *protocol.CNPeer, _account interface{}, pk SendError(LOGIN_DATABASE_ERROR) return err } - } else if err == db.ErrLoginInvalidPassword { + } else if errors.Is(err, db.ErrLoginInvalidPassword) { // respond with invalid password SendError(LOGIN_ID_AND_PASSWORD_DO_NOT_MATCH) return nil