M sdbapi/apierror/apierror.go => sdbapi/apierror/apierror.go +37 -20
@@ 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
+}
+
M sdbapi/db/game.go => sdbapi/db/game.go +2 -4
@@ 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
M sdbapi/db/user.go => sdbapi/db/user.go +4 -6
@@ 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
M sdbapi/handlers/mygames.go => sdbapi/handlers/mygames.go +1 -1
@@ 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
}