From c687efdd9cbc8b9b395d98a6c13d5acf308a39d9 Mon Sep 17 00:00:00 2001 From: LightJack05 <66321084+LightJack05@users.noreply.github.com> Date: Tue, 27 Jan 2026 15:41:52 +0100 Subject: [PATCH 1/2] Added error struct --- internal/handlers/error.go | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 internal/handlers/error.go diff --git a/internal/handlers/error.go b/internal/handlers/error.go new file mode 100644 index 0000000..1225f82 --- /dev/null +++ b/internal/handlers/error.go @@ -0,0 +1,5 @@ +package handlers + +type Error struct { + Error string `json:"error"` +} From 0ca4b1ae60cbd3b7b381ae193b90fbc803dee470 Mon Sep 17 00:00:00 2001 From: LightJack05 <66321084+LightJack05@users.noreply.github.com> Date: Tue, 27 Jan 2026 15:54:30 +0100 Subject: [PATCH 2/2] Update errors to use the new format --- internal/handlers/product/get.go | 15 ++++++++------- internal/handlers/product/getId.go | 15 ++++++++------- internal/health/get.go | 3 ++- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/internal/handlers/product/get.go b/internal/handlers/product/get.go index 845f5ea..e5bdee8 100644 --- a/internal/handlers/product/get.go +++ b/internal/handlers/product/get.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/SnackLog/database-api-wrapper/internal/database/product" + "github.com/SnackLog/database-api-wrapper/internal/handlers" "github.com/gin-gonic/gin" ) @@ -18,15 +19,15 @@ import ( // @Param q query string true "Search query (min 3 chars)" // @Param limit query int false "Limit results (default 10, max 100)" // @Success 200 {object} map[string][]product.Product -// @Failure 400 {object} map[string]string -// @Failure 401 {object} map[string]string -// @Failure 500 {object} map[string]string +// @Failure 400 {object} handlers.Error +// @Failure 401 {object} handlers.Error +// @Failure 500 {object} handlers.Error // @Security Bearer // @Router /products/search [get] func (p *ProductController) Get(c *gin.Context) { query := c.Query("q") if query == "" || len(query) < 3 { - c.JSON(http.StatusBadRequest, gin.H{"error": "Query parameter 'q' is required and must be at least 3 characters long"}) + c.JSON(http.StatusBadRequest, handlers.Error{Error: "Query parameter 'q' is required and must be at least 3 characters long"}) return } @@ -40,19 +41,19 @@ func (p *ProductController) Get(c *gin.Context) { if err != nil { log.Println(err) } - c.JSON(http.StatusBadRequest, gin.H{"error": "Query parameter 'limit' must be a positive integer less than 100"}) + c.JSON(http.StatusBadRequest, handlers.Error{Error: "Query parameter 'limit' must be a positive integer less than 100"}) return } products, err := product.SearchProductByName(p.DB, query, int(limit)) if err != nil { log.Println(err) - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()}) return } if products == nil { log.Println("Products was nil?!") - c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "products was nil."}) + c.AbortWithStatusJSON(http.StatusInternalServerError, handlers.Error{Error: "products was nil."}) return } diff --git a/internal/handlers/product/getId.go b/internal/handlers/product/getId.go index dccf8d8..1e768f0 100644 --- a/internal/handlers/product/getId.go +++ b/internal/handlers/product/getId.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/SnackLog/database-api-wrapper/internal/database/product" + "github.com/SnackLog/database-api-wrapper/internal/handlers" "github.com/gin-gonic/gin" ) @@ -16,28 +17,28 @@ import ( // @Produce json // @Param id path string true "Product ID" // @Success 200 {object} map[string]product.Product -// @Failure 400 {object} map[string]string -// @Failure 401 {object} map[string]string -// @Failure 404 {object} map[string]string -// @Failure 500 {object} map[string]string +// @Failure 400 {object} handlers.Error +// @Failure 401 {object} handlers.Error +// @Failure 404 {object} handlers.Error +// @Failure 500 {object} handlers.Error // @Security Bearer // @Router /products/{id} [get] func (p *ProductController) GetID(c *gin.Context) { id := c.Param("id") if id == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "ID is required"}) + c.JSON(http.StatusBadRequest, handlers.Error{Error: "ID is required"}) return } product, err := product.GetProductByID(p.DB, id) if err != nil { log.Println(err) - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()}) return } if product == nil { - c.JSON(http.StatusNotFound, gin.H{"error": "Product not found"}) + c.JSON(http.StatusNotFound, handlers.Error{Error: "Product not found"}) return } diff --git a/internal/health/get.go b/internal/health/get.go index 5e4182d..da4a98f 100644 --- a/internal/health/get.go +++ b/internal/health/get.go @@ -4,13 +4,14 @@ import ( "context" "net/http" + "github.com/SnackLog/database-api-wrapper/internal/handlers" "github.com/gin-gonic/gin" ) func (hc *HealthController) Get(c *gin.Context) { err := hc.DB.Ping(context.TODO(), nil) if err != nil { - c.AbortWithStatusJSON(http.StatusServiceUnavailable, gin.H{"error": "Database connection is down"}) + c.AbortWithStatusJSON(http.StatusServiceUnavailable, handlers.Error{Error: "Database connection is down"}) return } c.Status(http.StatusOK)