-
Notifications
You must be signed in to change notification settings - Fork 24
/
errors.go
61 lines (54 loc) · 1.19 KB
/
errors.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 web
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/tsuru/rpaas-operator/internal/pkg/rpaas"
)
func ErrorMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
err := next(c)
if err == nil {
return nil
}
if rpaas.IsValidationError(err) {
return &echo.HTTPError{Code: http.StatusBadRequest, Message: err}
}
if rpaas.IsConflictError(err) {
return &echo.HTTPError{Code: http.StatusConflict, Message: err}
}
if rpaas.IsNotFoundError(err) {
return &echo.HTTPError{Code: http.StatusNotFound, Message: err}
}
return err
}
}
func HTTPErrorHandler(err error, c echo.Context) {
var (
code = http.StatusInternalServerError
msg interface{}
)
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
msg = he.Message
if he.Internal != nil {
msg = fmt.Sprintf("%v, %v", err, he.Internal)
}
} else {
msg = err.Error()
}
if _, ok := msg.(string); ok {
msg = echo.Map{"message": msg}
}
c.Logger().Error(err)
if !c.Response().Committed {
if c.Request().Method == http.MethodHead {
err = c.NoContent(code)
} else {
err = c.JSON(code, msg)
}
if err != nil {
c.Logger().Error(err)
}
}
}