From c0611410e118719ac8dffabd10d6c50bc2d96703 Mon Sep 17 00:00:00 2001 From: siddhantloya Date: Tue, 20 Sep 2022 18:06:23 +0530 Subject: [PATCH] fix: error handling based on #1 --- server/controllers/authController.go | 41 ++++++++++++++++------------ server/db/dbConn.go | 2 ++ server/helpers/errors.go | 5 ++++ server/helpers/tokenHelper.go | 5 ++-- 4 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 server/helpers/errors.go diff --git a/server/controllers/authController.go b/server/controllers/authController.go index 079c857..db98ea6 100644 --- a/server/controllers/authController.go +++ b/server/controllers/authController.go @@ -1,6 +1,7 @@ package controllers import ( + "fmt" "log" "github.com/formatkaka/youtube-clone/db" @@ -13,9 +14,9 @@ func SignUp(ctx *gin.Context) { var req models.SignUpRequest if err := ctx.BindJSON(&req); err != nil { - log.Fatal("bind issue signup request obj") + log.Println("bind issue signup request obj : ", err) ctx.JSON(500, gin.H{ - "error": "bind issue signup request obj", + "error": fmt.Sprintf("bind issue signup request obj : %s", err.Error()), }) return } @@ -23,24 +24,24 @@ func SignUp(ctx *gin.Context) { hash, err := helpers.GenerateHash(req.Password) if err != nil { - log.Println(err) + log.Println("generate hash error in signup func ", err) ctx.JSON(200, gin.H{ - "error": err, + "error": fmt.Sprintf("generate hash error in signup func %s ", err), }) return } - log.Println("hash : ", hash) - client := db.GetClient() var id int64 query := "INSERT INTO USERS (name, email, password) VALUES ($1, $2, $3) RETURNING id" err = client.QueryRow(query, req.Name, req.Email, hash).Scan(&id) if err != nil { - log.Println(err) + // how to return error that email already exists in db ?? + // should I check if email exists and then put or try to analyse the error message + log.Println("unable to insert ", err) ctx.JSON(500, gin.H{ - "err": err, + "error": fmt.Sprintf("unable to insert %s ", err), }) return } @@ -48,9 +49,10 @@ func SignUp(ctx *gin.Context) { token, refreshToken, err := helpers.GenerateToken(req.Email, req.Name, id) if err != nil { - log.Println(err) + log.Println("signup request : ", err) ctx.JSON(500, gin.H{ - "err": err, + "error": fmt.Sprintf("signup request : %s", err), + "status": helpers.TOKEN_ERROR, }) return } @@ -66,9 +68,9 @@ func Login(ctx *gin.Context) { var req models.LoginRequest if err := ctx.BindJSON(&req); err != nil { - log.Fatal("bind issue signup request obj") + log.Println("bind issue login request obj : ", err) ctx.JSON(500, gin.H{ - "error": "bind issue signup request obj", + "error": fmt.Sprintf("bind issue login request obj : %s", err.Error()), }) return } @@ -89,11 +91,12 @@ func Login(ctx *gin.Context) { if rows.Next() { if err := rows.Scan(&id, &name, &hash); err != nil { - panic(err) + log.Println("ERROR: login rows Scan : ", err) } } else { ctx.JSON(500, gin.H{ - "error": "Email does not exist", + "error": "Email does not exist", + "status": helpers.EMAIL_NOT_EXIST, }) return } @@ -102,9 +105,10 @@ func Login(ctx *gin.Context) { token, refreshToken, err := helpers.GenerateToken(req.Email, name, id) if err != nil { - log.Println(err) + log.Println("login func : ", err) ctx.JSON(500, gin.H{ - "err": err, + "error": fmt.Sprintf("login function token : %s", err.Error()), + "status": helpers.TOKEN_ERROR, }) return } @@ -119,8 +123,9 @@ func Login(ctx *gin.Context) { return } - ctx.JSON(500, gin.H{ - "error": "Invalid email or password", + ctx.JSON(200, gin.H{ + "error": "Invalid email or password", + "errorcode": helpers.INVALID_LOGIN_CREDS, }) } diff --git a/server/db/dbConn.go b/server/db/dbConn.go index 1730e4e..eca04c2 100644 --- a/server/db/dbConn.go +++ b/server/db/dbConn.go @@ -32,6 +32,8 @@ func Init() { client, err = sql.Open("postgres", url) if err != nil { + // unable to connect to DB + // critical error log.Fatal(err) } diff --git a/server/helpers/errors.go b/server/helpers/errors.go new file mode 100644 index 0000000..4e8e9f9 --- /dev/null +++ b/server/helpers/errors.go @@ -0,0 +1,5 @@ +package helpers + +const INVALID_LOGIN_CREDS = "INVALID_LOGIN_CREDS" +const EMAIL_NOT_EXIST = "EMAIL_NOT_EXIST" +const TOKEN_ERROR = "TOKEN_ERROR" diff --git a/server/helpers/tokenHelper.go b/server/helpers/tokenHelper.go index 279c7a4..80d411d 100644 --- a/server/helpers/tokenHelper.go +++ b/server/helpers/tokenHelper.go @@ -1,6 +1,7 @@ package helpers import ( + "fmt" "log" "os" "time" @@ -37,14 +38,14 @@ func GenerateToken(email string, name string, uid int64) (string, string, error) if err != nil { log.Println(err) - return "", "", err + return "", "", fmt.Errorf("token generation error in GenerateToken : %w", err) } refreshToken, err := jwt.NewWithClaims(jwt.SigningMethodHS256, refreshClaims).SignedString([]byte(SECRET_KEY)) if err != nil { log.Println(err) - return "", "", err + return "", "", fmt.Errorf("refresh token generation error in GenerateToken : %w", err) } return token, refreshToken, nil