-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_middleware_error.go
61 lines (55 loc) · 1.36 KB
/
http_middleware_error.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package core
import (
"fmt"
"github.com/getsentry/sentry-go"
"github.com/go-errors/errors"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
)
func RecoverWithConfig(env IENV, config middleware.RecoverConfig) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = middleware.DefaultRecoverConfig.Skipper
}
if config.StackSize == 0 {
config.StackSize = middleware.DefaultRecoverConfig.StackSize
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
cc := c.(IHTTPContext)
if config.Skipper(c) {
return next(c)
}
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
errWrap := errors.Wrap(err, 1)
if !config.DisablePrintStack {
stack := errWrap.ErrorStack()
fmt.Println(stack)
}
cc.Log().Error(errWrap)
cc.Error(err)
}
}()
return next(c)
}
}
}
func HandleError(err error, c echo.Context) {
CaptureErrorEcho(c, sentry.LevelError, err)
c.JSON(http.StatusInternalServerError, map[string]interface{}{
"code": "INTERNAL_SERVER_ERROR",
"message": err.Error(),
})
}
func HandleNotFound(c echo.Context) error {
return c.JSON(http.StatusNotFound, map[string]interface{}{
"code": "URL_NOT_FOUND",
"message": "url not found",
})
}