break helpers

This commit is contained in:
celogeek 2022-02-05 11:21:45 +01:00
parent f710c19d65
commit 8ad8ea30df
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 46 additions and 36 deletions

View File

@ -0,0 +1,14 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Service) RequireBody(c *gin.Context) {
if c.Request.Method == "POST" && c.Request.ContentLength == 0 {
s.Error(c, http.StatusBadRequest, ErrReqMissingBody)
return
}
}

View File

@ -0,0 +1,15 @@
package api
import (
"bytes"
"encoding/json"
)
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())
}

View File

@ -1,36 +0,0 @@
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(),
})
}

View File

@ -0,0 +1,17 @@
package api
import (
"net/http"
"github.com/gin-gonic/gin"
)
func (s *Service) Recovery(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
s.Logger.Print("[PANIC]", err)
s.Error(c, http.StatusInternalServerError, ErrUnexpected)
}
}()
c.Next()
}