package api import ( "encoding/json" "git.aiterp.net/lucifer/new-server/models" "github.com/gin-gonic/gin" ) var errorMap = map[error]int{ models.ErrInvalidName: 400, } type response struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data"` } func handler(fun func(c *gin.Context) (interface{}, error)) gin.HandlerFunc { return func(c *gin.Context) { val, err := fun(c) if err != nil { errCode := errorMap[err] if errCode == 0 { errCode = 500 } c.JSON(errCode, response{ Code: errCode, Message: err.Error(), }) return } c.JSON(200, val) } } func parseBody(c *gin.Context, target interface{}) error { err := json.NewDecoder(c.Request.Body).Decode(target) if err != nil { return models.ErrBadInput } return nil }