2022-05-09 09:49:11 +02:00

38 lines
533 B
Go

package photosapi
import (
"errors"
"strings"
"github.com/gin-gonic/gin"
)
var (
ErrReqMissingBody = errors.New("missing body")
)
func (s *Service) HandleError(c *gin.Context) {
c.Next()
err := c.Errors.Last()
if err == nil {
return
}
details := err.Error()
if details == "EOF" {
details = "missing body"
}
switch err.Type {
case gin.ErrorTypeBind:
c.JSON(-1, gin.H{
"error": "binding error",
"details": strings.Split(details, "\n"),
})
default:
c.JSON(-1, gin.H{
"error": details,
})
}
}