-
Notifications
You must be signed in to change notification settings - Fork 0
Clean up error format #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package handlers | ||
|
|
||
| type Error struct { | ||
| Error string `json:"error"` | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||
|
Comment on lines
19
to
+24
|
||||||
| // @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()}) | ||||||
|
||||||
| c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()}) | |
| c.JSON(http.StatusInternalServerError, handlers.Error{Error: "internal server error"}) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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()}) | ||||||
|
||||||
| c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()}) | |
| c.JSON(http.StatusInternalServerError, handlers.Error{Error: "Internal server error"}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new response type name/field combo results in awkward usage like
handlers.Error{Error: ...}and can be confused with Go’s built-inerrorconcept. Consider renaming the struct to something likeErrorResponse(keeping the JSON tagerror) and using a field name likeMessageto make call sites clearer.