There's a mismatch between your API documentation and your implementation. Let me explain how to resolve this inconsistency:
The Discrepancy
Your documentation states that the API returns an entities.APIError object on a 500 error:
// @Failure 500 {object} entities.APIError
But your implementation returns a simple JSON object with an "error" field:
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
How to Align Documentation and Implementation
You have two options:
Option 1: Update Your Implementation to Match Documentation
Define and use the entities.APIError structure in your response:
// In your entities package
type APIError struct {
Error string `json:"error"`
Code int `json:"code"`
Description string `json:"description,omitempty"`
}
// In your handler
func YourHandler(c *gin.Context) {
// ...
if err != nil {
apiError := entities.APIError{
Error: "Internal Server Error",
Code: http.StatusInternalServerError,
Description: err.Error(),
}
c.JSON(http.StatusInternalServerError, apiError)
return
}
// ...
}
Option 2: Update Your Documentation to Match Implementation
Change your documentation to reflect the actual response structure:
// @Failure 500 {object} gin.H "{"error": "error message"}"
Best Practices for Error Handling in Gin
Based on the search results and Gin best practices, here are some recommendations:
1. Use Consistent Error Structures
Create a standardized error structure for your API responses[1][5]:
type APIError struct {
Code int `json:"code"`
Message string `json:"message"`
Description string `json:"description,omitempty"`
}
func NewAPIError(code int, message, description string) APIError {
return APIError{
Code: code,
Message: message,
Description: description,
}
}
2. Implement Middleware for Centralized Error Handling
Create an error handling middleware to maintain consistency across your application[3][7][9]:
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if len(c.Errors) > 0 {
err := c.Errors.Last()
switch e := err.Err.(type) {
case APIError:
c.JSON(e.Code, e)
default:
c.JSON(http.StatusInternalServerError, APIError{
Code: http.StatusInternalServerError,
Message: "Internal Server Error",
Description: e.Error(),
})
}
}
}
}
Register the middleware in your main.go:
router := gin.Default()
router.Use(ErrorHandler())
3. Use c.Error() in Your Handlers
In your handlers, use c.Error() to add errors to the context[2][4]:
func YourHandler(c *gin.Context) {
// ...
if err != nil {
apiErr := NewAPIError(http.StatusInternalServerError, "Internal Server Error", err.Error())
c.Error(apiErr)
return
}
// ...
}
Recommendation
For better maintainability and consistency, I recommend Option 1 - updating your implementation to match your documentation. Using a structured error type like entities.APIError provides more information to API consumers and makes your error handling more consistent across your application.
This approach, combined with a middleware-based error handling system, will give you a clean, centralized way to manage errors throughout your application.