Skip to content

Clean up error format#20

Merged
LightJack05 merged 2 commits intomainfrom
clean-error-format
Jan 27, 2026
Merged

Clean up error format#20
LightJack05 merged 2 commits intomainfrom
clean-error-format

Conversation

@LightJack05
Copy link
Member

No description provided.

@LightJack05 LightJack05 requested a review from Ferrari78 January 27, 2026 14:55
@LightJack05 LightJack05 self-assigned this Jan 27, 2026
Copilot AI review requested due to automatic review settings January 27, 2026 14:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Standardizes JSON error responses across selected endpoints by introducing a shared error payload type.

Changes:

  • Added a shared handlers.Error struct for consistent {"error": "..."}
  • Updated health + product handlers to return the new error payload type
  • Updated Swagger annotations for product endpoints to reference the new error model

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
internal/health/get.go Uses shared error payload for health DB ping failure response
internal/handlers/product/getId.go Uses shared error payload + updates Swagger failure response types
internal/handlers/product/get.go Uses shared error payload + updates Swagger failure response types
internal/handlers/error.go Introduces the shared error payload type

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 19 to +24
// @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
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API doc says limit has a max of 100, but the validation rejects limit >= 100 and the error message says it must be less than 100 (i.e., max 99). Update either the docs or the validation/message so they are consistent.

Copilot uses AI. Check for mistakes.
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()})
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 500 response body includes the raw internal error string (err.Error()), which can leak implementation/database details to clients. Consider returning a generic message (and optionally a stable error code) while keeping the detailed error only in logs.

Suggested change
c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()})
c.JSON(http.StatusInternalServerError, handlers.Error{Error: "Internal server error"})

Copilot uses AI. Check for mistakes.
Comment on lines +3 to +4
type Error struct {
Error string `json:"error"`
Copy link

Copilot AI Jan 27, 2026

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-in error concept. Consider renaming the struct to something like ErrorResponse (keeping the JSON tag error) and using a field name like Message to make call sites clearer.

Suggested change
type Error struct {
Error string `json:"error"`
type ErrorResponse struct {
Message string `json:"error"`

Copilot uses AI. Check for mistakes.
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()})
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 500 response body includes the raw internal error string (err.Error()), which can leak implementation/database details to clients. Consider returning a generic message (and optionally a stable error code) while keeping the detailed error only in logs.

Suggested change
c.JSON(http.StatusInternalServerError, handlers.Error{Error: err.Error()})
c.JSON(http.StatusInternalServerError, handlers.Error{Error: "internal server error"})

Copilot uses AI. Check for mistakes.
@LightJack05 LightJack05 merged commit 6d7e39d into main Jan 27, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants