From e53650f959534137eec8b14804097b4899b546fd Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 26 Jan 2023 13:56:21 +0200 Subject: [PATCH] feat(sdbapi): new error handling --- sdbapi/apierror/apierror.go | 57 ++++++++++++++++++++++++------------- sdbapi/db/game.go | 6 ++-- sdbapi/db/user.go | 10 +++---- sdbapi/handlers/mygames.go | 2 +- 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/sdbapi/apierror/apierror.go b/sdbapi/apierror/apierror.go index 805e604..8f3b165 100644 --- a/sdbapi/apierror/apierror.go +++ b/sdbapi/apierror/apierror.go @@ -8,24 +8,41 @@ package apierror -const ( - Placeholder string = "placeholder" - InvalidInput string = "invalid input" - UserNotFound string = "user not found" - InvalidCred string = "invalid credentials" - NewJWTError string = "jwt creation error" - UsernameTooShort string = "username should not be shorter than 3 characters" - EmailInvalid string = "invalid email address" - PasswordTooShort string = "password should not be shorter than 8 characters" - PasswordHashFailed string = "password hash failed" - UserCreationFailed string = "user creation failed" - NotAuthorized string = "not authorized" - GameNotFound string = "game not found" - GameCreationFailed string = "game creation failed" - GetAllFormingFailed string = "something went wrong while fetching all forming games" - GameStatePatchFailed string = "failed to patch game state" - NoGamesForUser = "no games found for user" - GameNotForming string = "game is not forming" - CannotJoinOwnGame string = "can not join own game" - GameFull string = "game is full" +// general errors +var ( + Placeholder APIError = APIError{1000, "Placeholder", "placeholder"} + InvalidInput APIError = APIError{1001, "InvalidInput", "invalid input"} + NotAuthorized APIError = APIError{1002, "NotAuthorized", "not authorized"} ) + +// user related errors +var ( + UserNotFound APIError = APIError{2000, "UserNotFound", "user not found"} + InvalidCred APIError = APIError{2001, "InvalidCred", "invalid credentials"} + NewJWTError APIError = APIError{2003, "NewJWTError", "jwt creation error"} + // login errors + EmailInvalid APIError = APIError{2100, "EmailInvalid", "invalid email address"} + UsernameTooShort APIError = APIError{2101, "UsernameTooShort", "username should not be shroter than 3 characters"} + PasswordTooShort APIError = APIError{2102, "PasswordTooShort", "password should not be shorter than 8 characters"} + PasswordHashFailed APIError = APIError{2103, "PasswordHashFailed", "password hash failed"} + UserCreationFailed APIError = APIError{2104, "UserCreationFailed", "user creation failed"} +) + +// game related errors +var ( + GameNotFound APIError = APIError{3000, "GameNotFound", "game not found"} + GameCreationFailed APIError = APIError{3001, "GameCreationFailed", "game creation failed"} + GetAllFormingFailed APIError = APIError{3002, "GetAllFormingFailed", "failed to get all forming games"} + GameStatePatchFailed APIError = APIError{3003, "GameStatePatchFailed", "failed to patch game state"} + NoGamesForUser APIError = APIError{3004, "NoGamesForUser", "no games found for user"} + GameNotForming APIError = APIError{3005, "GameNotForming", "game is not forming"} + CannotJoinOwnGame APIError = APIError{3006, "CannotJoinOwnGame", "can not join own game"} + GameFull APIError = APIError{3007, "GameFull", "game is full"} +) + +type APIError struct { + ID uint16 + Name string + Description string +} + diff --git a/sdbapi/db/game.go b/sdbapi/db/game.go index 2246c84..f00c48d 100644 --- a/sdbapi/db/game.go +++ b/sdbapi/db/game.go @@ -11,16 +11,14 @@ package db import ( "api/apierror" "api/models" - - "errors" ) // get games for a specific user with email -func GetGamesForUser(id string) ([]models.Game, error) { +func GetGamesForUser(id string) ([]models.Game, *apierror.APIError) { var games []models.Game game_records := DbConn.Where("p1 = ? OR p2 = ?", id, id).Find(&games) if game_records.Error != nil { - return []models.Game{}, errors.New(apierror.NoGamesForUser) + return []models.Game{}, &apierror.NoGamesForUser } return games, nil diff --git a/sdbapi/db/user.go b/sdbapi/db/user.go index 7090354..d978bcc 100644 --- a/sdbapi/db/user.go +++ b/sdbapi/db/user.go @@ -10,28 +10,26 @@ package db import ( "api/models" - "api/apierror" - "errors" ) // get a user with an ID -func GetUserByID(id string) (models.User, error) { +func GetUserByID(id string) (models.User, *apierror.APIError) { var user models.User user_record := DbConn.Where("id = ?", id).First(&user) if user_record.Error != nil { - return models.User{}, errors.New(apierror.UserNotFound) + return models.User{}, &apierror.UserNotFound } return user, nil } // get a user with an email address -func GetUserByEmail(email string) (models.User, error) { +func GetUserByEmail(email string) (models.User, *apierror.APIError) { var user models.User user_record := DbConn.Where("email = ?", email).First(&user) if user_record.Error != nil { - return models.User{}, errors.New(apierror.UserNotFound) + return models.User{}, &apierror.UserNotFound } return user, nil diff --git a/sdbapi/handlers/mygames.go b/sdbapi/handlers/mygames.go index cd67010..acdda70 100644 --- a/sdbapi/handlers/mygames.go +++ b/sdbapi/handlers/mygames.go @@ -28,7 +28,7 @@ func MyGames(c *gin.Context) { games, err := db.GetGamesForUser(user.ID) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"error": err}) c.Abort() return } -- 2.44.1