From 9a0b81fba3312c1e96f367a18a10bb76325f95db Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Mon, 6 Jan 2025 03:53:08 +0300 Subject: [PATCH] refactor(api): move postgres error handling into a separate function in a separate file --- backend/api/handlers.go | 30 ++++-------------------------- backend/api/utils.go | 22 ++++++++++++++++++++++ backend/db/db.go | 9 --------- 3 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 backend/api/utils.go diff --git a/backend/api/handlers.go b/backend/api/handlers.go index 893f3db..3d60e30 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -25,19 +25,8 @@ func userAuth(c *gin.Context) { } user, err := db.UserAuth(context.Background(), credentials.Login, credentials.Password) if err != nil { - pqErr := db.CastToPgError(err) - if pqErr == nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - statusStr := pqErr.Message[:3] - msg := pqErr.Message[4:] - status, _err := strconv.ParseInt(statusStr, 10, 0) - if _err == nil { - c.JSON(int(status), gin.H{"error": msg}) - } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - } + status, message := HandlePgError(err) + c.JSON(status, gin.H{"error": message}) return } session := sessions.Default(c) @@ -92,19 +81,8 @@ func quotesGet(c *gin.Context) { } quotes, err := db.QuotesGet(c, user_id, filter, sort, int(limit), int(offset)) if err != nil { - pqErr := db.CastToPgError(err) - if pqErr == nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - statusStr := pqErr.Message[:3] - msg := pqErr.Message[4:] - status, _err := strconv.ParseInt(statusStr, 10, 0) - if _err == nil { - c.JSON(int(status), gin.H{"error": msg}) - } else { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - } + status, message := HandlePgError(err) + c.JSON(status, gin.H{"error": message}) return } c.JSON(http.StatusOK, quotes) diff --git a/backend/api/utils.go b/backend/api/utils.go new file mode 100644 index 0000000..2b1a3ea --- /dev/null +++ b/backend/api/utils.go @@ -0,0 +1,22 @@ +package api + +import ( + "strconv" + + "github.com/jackc/pgx/v5/pgconn" +) + +func HandlePgError(err error) (int, string) { + pgErr, ok := err.(*pgconn.PgError) + if !ok { + return 500, err.Error() + } + statusStr := pgErr.Message[:3] + message := pgErr.Message[4:] + status, err := strconv.ParseInt(statusStr, 10, 0) + if err == nil { + return int(status), message + } else { + return 500, message + } +} diff --git a/backend/db/db.go b/backend/db/db.go index f0d7f1a..3980a0a 100644 --- a/backend/db/db.go +++ b/backend/db/db.go @@ -7,7 +7,6 @@ import ( "time" "github.com/H1K0/SkazaNull/models" - "github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgxpool" ) @@ -31,14 +30,6 @@ func InitDB(connString string) error { return nil } -func CastToPgError(err error) *pgconn.PgError { - pqErr, ok := err.(*pgconn.PgError) - if ok { - return pqErr - } - return nil -} - //#region User func UserAuth(ctx context.Context, login string, password string) (user models.User, err error) {