refactor(api): move postgres error handling into a separate function in a separate file
This commit is contained in:
parent
c7df4a1960
commit
9a0b81fba3
@ -25,19 +25,8 @@ func userAuth(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
user, err := db.UserAuth(context.Background(), credentials.Login, credentials.Password)
|
user, err := db.UserAuth(context.Background(), credentials.Login, credentials.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pqErr := db.CastToPgError(err)
|
status, message := HandlePgError(err)
|
||||||
if pqErr == nil {
|
c.JSON(status, gin.H{"error": message})
|
||||||
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()})
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
session := sessions.Default(c)
|
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))
|
quotes, err := db.QuotesGet(c, user_id, filter, sort, int(limit), int(offset))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pqErr := db.CastToPgError(err)
|
status, message := HandlePgError(err)
|
||||||
if pqErr == nil {
|
c.JSON(status, gin.H{"error": message})
|
||||||
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()})
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, quotes)
|
c.JSON(http.StatusOK, quotes)
|
||||||
|
|||||||
22
backend/api/utils.go
Normal file
22
backend/api/utils.go
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,7 +7,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/H1K0/SkazaNull/models"
|
"github.com/H1K0/SkazaNull/models"
|
||||||
"github.com/jackc/pgx/v5/pgconn"
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,14 +30,6 @@ func InitDB(connString string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CastToPgError(err error) *pgconn.PgError {
|
|
||||||
pqErr, ok := err.(*pgconn.PgError)
|
|
||||||
if ok {
|
|
||||||
return pqErr
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//#region User
|
//#region User
|
||||||
|
|
||||||
func UserAuth(ctx context.Context, login string, password string) (user models.User, err error) {
|
func UserAuth(ctx context.Context, login string, password string) (user models.User, err error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user