You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 lines
825 B

package api
import (
"encoding/json"
"git.aiterp.net/lucifer/server3/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
}