Merge branch 'development' of https://gitlab.com/mbugroup/lti-api into Feat/BE/Expense_adjust_approval_flow

This commit is contained in:
aguhh18
2026-01-11 21:19:35 +07:00
32 changed files with 507 additions and 205 deletions
+38
View File
@@ -2,11 +2,14 @@ package utils
import (
"errors"
"strings"
"gitlab.com/mbugroup/lti-api.git/internal/common/validation"
"gitlab.com/mbugroup/lti-api.git/internal/response"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgconn"
pgconnv5 "github.com/jackc/pgx/v5/pgconn"
)
func ErrorHandler(c *fiber.Ctx, err error) error {
@@ -14,6 +17,10 @@ func ErrorHandler(c *fiber.Ctx, err error) error {
return response.Error(c, fiber.StatusBadRequest, message, nil)
}
if statusCode, message := mapPgError(err); statusCode != 0 {
return response.Error(c, statusCode, message, nil)
}
var fiberErr *fiber.Error
if errors.As(err, &fiberErr) {
return response.Error(c, fiberErr.Code, fiberErr.Message, nil)
@@ -26,6 +33,37 @@ func NotFoundHandler(c *fiber.Ctx) error {
return response.Error(c, fiber.StatusNotFound, "Endpoint Not Found", nil)
}
func mapPgError(err error) (int, string) {
code, message := getPgErrorDetails(err)
if code == "" {
return 0, ""
}
switch code {
case "23503":
return fiber.StatusConflict, "Data tidak bisa dihapus karena masih digunakan oleh data lain."
case "P0001":
if strings.HasPrefix(message, "Cannot soft delete") {
return fiber.StatusConflict, "Data tidak bisa dihapus karena masih digunakan oleh data lain."
}
}
return 0, ""
}
func getPgErrorDetails(err error) (string, string) {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.Code, pgErr.Message
}
var pgErrV5 *pgconnv5.PgError
if errors.As(err, &pgErrV5) {
return pgErrV5.Code, pgErrV5.Message
}
return "", ""
}
func BadRequest(msg string) error {
return fiber.NewError(fiber.StatusBadRequest, msg)