gin custom error messages examples
Example: gin custom error messages
package util
import (
"github.com/gin-gonic/gin"
)
type Responses struct {
StatusCode int
Method string
Message string
Data interface{}
}
func APIResponse(ctx *gin.Context, Message string, StatusCode int, Method string, Data interface{}) {
jsonResponse := Responses{
StatusCode: StatusCode,
Method: Method,
Message: Message,
Data: Data,
}
if StatusCode >= 400 {
ctx.JSON(StatusCode, jsonResponse)
defer ctx.AbortWithStatus(StatusCode)
} else {
ctx.JSON(StatusCode, jsonResponse)
}
}