break helpers
This commit is contained in:
parent
f710c19d65
commit
8ad8ea30df
14
internal/photos/api/check_body.go
Normal file
14
internal/photos/api/check_body.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
15
internal/photos/api/dump.go
Normal file
15
internal/photos/api/dump.go
Normal 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())
|
||||||
|
}
|
@ -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(),
|
|
||||||
})
|
|
||||||
}
|
|
17
internal/photos/api/recovery.go
Normal file
17
internal/photos/api/recovery.go
Normal 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()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user