/
error_handling.go
41 lines (35 loc) · 1004 Bytes
/
error_handling.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
package middleware
import (
"fmt"
"net/http"
h "github.com/RedHatInsights/sources-api-go/middleware/headers"
"github.com/RedHatInsights/sources-api-go/util"
"github.com/labstack/echo/v4"
)
func HandleErrors(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
err := next(c)
if err != nil {
var statusCode int
var message interface{}
switch err.(type) {
case util.ErrNotFound:
statusCode = http.StatusNotFound
message = util.ErrorDocWithoutLogging(err.Error(), "404")
case util.ErrBadRequest:
statusCode = http.StatusBadRequest
message = util.ErrorDocWithoutLogging(err.Error(), "400")
default:
c.Logger().Error(err)
uuid, ok := c.Get(h.InsightsRequestID).(string)
if !ok {
uuid = ""
}
statusCode = http.StatusInternalServerError
message = util.ErrorDocWithRequestId(fmt.Sprintf("Internal Server Error: %v", err.Error()), "500", uuid)
}
return c.JSON(statusCode, message)
}
return nil
}
}