use proper errors for db.Err

- switch to using errors.Is where applicable
This commit is contained in:
CPunch 2023-11-29 19:52:10 -06:00
parent 18a6c5ab42
commit d0346b2382
3 changed files with 8 additions and 6 deletions

View File

@ -1,7 +1,7 @@
package db package db
import ( import (
"fmt" "errors"
"log" "log"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -44,8 +44,8 @@ func (db *DBHandler) NewAccount(Login, Password string) (*Account, error) {
} }
var ( var (
ErrLoginInvalidID = fmt.Errorf("invalid Login ID") ErrLoginInvalidID = errors.New("invalid Login ID")
ErrLoginInvalidPassword = fmt.Errorf("invalid ID && Password combo") ErrLoginInvalidPassword = errors.New("invalid ID && Password combo")
) )
func (db *DBHandler) TryLogin(Login, Password string) (*Account, error) { func (db *DBHandler) TryLogin(Login, Password string) (*Account, error) {

View File

@ -2,6 +2,7 @@ package db_test
import ( import (
"context" "context"
"errors"
"os" "os"
"testing" "testing"
@ -50,7 +51,7 @@ func TestDBAccount(t *testing.T) {
t.Error("account username is not test") 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") t.Error("expected ErrLoginInvalidPassword")
} }
} }

View File

@ -2,6 +2,7 @@ package login
import ( import (
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
@ -79,7 +80,7 @@ func (server *LoginServer) Login(peer *protocol.CNPeer, _account interface{}, pk
// attempt login // attempt login
account, err := server.dbHndlr.TryLogin(loginPkt.SzID, loginPkt.SzPassword) 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 // 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) account, err = server.dbHndlr.NewAccount(loginPkt.SzID, loginPkt.SzPassword)
if err != nil { if err != nil {
@ -87,7 +88,7 @@ func (server *LoginServer) Login(peer *protocol.CNPeer, _account interface{}, pk
SendError(LOGIN_DATABASE_ERROR) SendError(LOGIN_DATABASE_ERROR)
return err return err
} }
} else if err == db.ErrLoginInvalidPassword { } else if errors.Is(err, db.ErrLoginInvalidPassword) {
// respond with invalid password // respond with invalid password
SendError(LOGIN_ID_AND_PASSWORD_DO_NOT_MATCH) SendError(LOGIN_ID_AND_PASSWORD_DO_NOT_MATCH)
return nil return nil