diff --git a/backend/.golangci.yml b/backend/.golangci.yml index 5912633..20ba8fc 100644 --- a/backend/.golangci.yml +++ b/backend/.golangci.yml @@ -3,13 +3,13 @@ linters: default: standard enable: - whitespace - - wrapcheck - tagalign - reassign - bodyclose - contextcheck - containedctx - godot + - usestdlibvars formatters: settings: diff --git a/backend/handlers/bullets/handler.go b/backend/handlers/bullets/handler.go index d4a0332..5bac5f5 100644 --- a/backend/handlers/bullets/handler.go +++ b/backend/handlers/bullets/handler.go @@ -27,9 +27,9 @@ func Photo(c echo.Context) error { }() id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } byId, err := db.Bullets.GetBulletById(context.Background(), *uid) @@ -57,12 +57,12 @@ func Delete(c echo.Context) error { }() id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } - err := db.Bullets.DeleteBullet(context.Background(), *uid) + err = db.Bullets.DeleteBullet(context.Background(), *uid) if err != nil { return err } @@ -77,9 +77,9 @@ func Put(c echo.Context) error { }() id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } byId, err := db.Bullets.GetBulletById(context.Background(), *uid) @@ -109,9 +109,9 @@ func Put(c echo.Context) error { weight, _ := strconv.ParseInt(c.FormValue("weight"), 10, 32) diameter, _ := strconv.ParseInt(c.FormValue("diameter"), 10, 32) manufacturerId := c.FormValue("manufacturer_id") - manufacturerUid := handlers.ParseUuidOrBadRequest(c, manufacturerId) - if manufacturerUid == nil { - return nil + manufacturerUid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId) + if err == nil || manufacturerUid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } name := c.FormValue("name") @@ -161,9 +161,9 @@ func Get(c echo.Context) error { if c.Param("id") != "" { id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } byId, err := db.Bullets.GetBulletById(context.Background(), *uid) @@ -258,9 +258,9 @@ func Post(c echo.Context) error { _ = db.Db.Close(context.Background()) }() - manufacturerUid := handlers.ParseUuidOrBadRequest(c, manufacturerId) - if manufacturerUid == nil { - return nil + manufacturerUid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId) + if err != nil || manufacturerUid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } manufacturer, err := db.Manufacturer.GetById(context.Background(), *manufacturerUid) diff --git a/backend/handlers/cartridge/handler.go b/backend/handlers/cartridge/handler.go index 85c9441..38448f6 100644 --- a/backend/handlers/cartridge/handler.go +++ b/backend/handlers/cartridge/handler.go @@ -87,12 +87,12 @@ func Delete(c echo.Context) error { _ = db.Db.Close(context.Background()) }() - uid := handlers.ParseUuidOrBadRequest(c, c.Param("id")) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, c.Param("id")) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "invalid id") } - err := db.Loads.DeleteCartridge(context.Background(), *uid) + err = db.Loads.DeleteCartridge(context.Background(), *uid) if err != nil { return err diff --git a/backend/handlers/manufacturer/handler.go b/backend/handlers/manufacturer/handler.go index f00e4c7..18a0249 100644 --- a/backend/handlers/manufacturer/handler.go +++ b/backend/handlers/manufacturer/handler.go @@ -27,9 +27,9 @@ func Get(c echo.Context) error { if c.Param("id") != "" { id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } byId, err := db.Manufacturer.GetById(context.Background(), *uid) @@ -80,12 +80,12 @@ func Delete(c echo.Context) error { }() id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } - _, err := db.Manufacturer.GetById(context.Background(), *uid) + _, err = db.Manufacturer.GetById(context.Background(), *uid) if err != nil { return echo.NewHTTPError(http.StatusNotFound, "Not found") } @@ -107,12 +107,12 @@ func Post(c echo.Context) error { if c.Param("id") != "" { id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID format") } - _, err := db.Manufacturer.GetById(context.Background(), *uid) + _, err = db.Manufacturer.GetById(context.Background(), *uid) if err != nil { return echo.NewHTTPError(http.StatusNotFound, "Not found") } diff --git a/backend/handlers/primers/handler.go b/backend/handlers/primers/handler.go index 2ba7b01..12e9d78 100644 --- a/backend/handlers/primers/handler.go +++ b/backend/handlers/primers/handler.go @@ -3,6 +3,7 @@ package primers import ( "context" "encoding/json" + "fmt" "git.siteworxpro.com/reloading-manager/backend/database" "git.siteworxpro.com/reloading-manager/backend/handlers" "git.siteworxpro.com/reloading-manager/backend/models/primers" @@ -45,9 +46,9 @@ func Post(c echo.Context) error { if c.Param("id") != "" { id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID.") } p, err := db.Primer.GetPrimerById(context.Background(), *uid) @@ -60,9 +61,9 @@ func Post(c echo.Context) error { } if c.FormValue("manufacturer_id") != "" { - mid := handlers.ParseUuidOrBadRequest(c, c.FormValue("manufacturer_id")) - if mid == nil { - return nil + mid, err := handlers.ParseUuidOrBadRequest(c, c.FormValue("manufacturer_id")) + if err != nil || mid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid Manufacturer ID.") } p.ManufacturerID = *mid @@ -121,9 +122,9 @@ func Post(c echo.Context) error { newUuid := uuid.New().String() uid, _ := handlers.ParseUuid(newUuid) - mid := handlers.ParseUuidOrBadRequest(c, manufacturerId) - if mid == nil { - return nil + mid, err := handlers.ParseUuidOrBadRequest(c, manufacturerId) + if err != nil || mid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid Manufacturer ID.") } err = db.Primer.InsertPrimer(context.Background(), primers.InsertPrimerParams{ @@ -155,9 +156,9 @@ func Photo(c echo.Context) error { }() id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID.") } byId, err := db.Primer.GetPrimerById(context.Background(), *uid) @@ -185,14 +186,14 @@ func Get(c echo.Context) error { if c.Param("id") != "" { id := c.Param("id") - uid := handlers.ParseUuidOrBadRequest(c, id) - if uid == nil { - return nil + uid, err := handlers.ParseUuidOrBadRequest(c, id) + if err != nil || uid == nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid UUID.") } row, err := db.Primer.GetPrimerById(context.Background(), *uid) if err != nil { - return err + return fmt.Errorf("primer not found: %v", err) } return c.JSON(http.StatusOK, handlers.Response[PrimerResponse]{ diff --git a/backend/handlers/uuid.go b/backend/handlers/uuid.go index c43657d..43f98c0 100644 --- a/backend/handlers/uuid.go +++ b/backend/handlers/uuid.go @@ -1,6 +1,7 @@ package handlers import ( + "fmt" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" "github.com/labstack/echo/v4" @@ -23,7 +24,7 @@ func ParseUuidOrEmpty(s string) *pgtype.UUID { func ParseUuid(s string) (*pgtype.UUID, error) { uid, err := uuid.Parse(s) if err != nil { - return nil, err + return nil, fmt.Errorf("invalid UUID format: %v", err) } return &pgtype.UUID{ @@ -32,13 +33,11 @@ func ParseUuid(s string) (*pgtype.UUID, error) { }, nil } -func ParseUuidOrBadRequest(c echo.Context, s string) *pgtype.UUID { +func ParseUuidOrBadRequest(c echo.Context, s string) (*pgtype.UUID, error) { uid, err := ParseUuid(s) if err != nil { - _ = BadRequest(c, "Invalid UUID.") - - return nil + return nil, BadRequest(c, fmt.Sprintf("invalid UUID. %v", err)) } - return uid + return uid, nil }