-
Notifications
You must be signed in to change notification settings - Fork 8
/
recovery.go
36 lines (32 loc) · 899 Bytes
/
recovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package recovery
import (
"fmt"
"runtime/debug"
"github.com/PolarPanda611/trinitygo/application"
"github.com/PolarPanda611/trinitygo/httputil"
"github.com/gin-gonic/gin"
"google.golang.org/grpc/codes"
)
// New runtime middleware
func New(app application.Application) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// when stack finishes
logMessage := fmt.Sprintf("Recovered from HTTP Request %v %v \n", c.Request.Method, c.Request.URL)
logMessage += fmt.Sprintf("Trace: %s\n", err)
logMessage += fmt.Sprintf("\n%s", debug.Stack())
app.Logger().Warn(logMessage)
c.AbortWithStatusJSON(400, httputil.ResponseData{
Status: 400,
Err: map[string]string{
"code": codes.Internal.String(),
"message": fmt.Sprintf("Internal err : %v", err),
},
})
return
}
}()
c.Next()
}
}