2022-02-01 09:40:27 +01:00

37 lines
650 B
Go

package api
import (
"bytes"
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Service) Recovery(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
s.Logger.Println("[PANIC]", err)
c.JSON(http.StatusInternalServerError, gin.H{
"error": "an unexpected error occur",
})
}
}()
c.Next()
}
func (s *Service) Dump(o interface{}) {
b := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(b)
enc.SetIndent("", " ")
enc.Encode(o)
s.Logger.Printf("%s", b.Bytes())
}
func (s *Service) Error(c *gin.Context, code int, err error) {
c.AbortWithStatusJSON(code, gin.H{
"error": err.Error(),
})
}