-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror.go
37 lines (33 loc) · 850 Bytes
/
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
package http
import (
"fmt"
"go.uber.org/zap"
"github.com/labstack/echo/v4"
"github.com/mcorbin/corbierror"
)
func errorHandler(logger *zap.Logger) func(err error, c echo.Context) {
return func(err error, c echo.Context) {
logger.Error(fmt.Sprintf("HTTP error: %s", err.Error()))
if he, ok := err.(*echo.HTTPError); ok {
if he.Code == 401 {
err := c.JSON(401, corbierror.New("Unauthorized", corbierror.Unauthorized, true))
if err != nil {
logger.Error(err.Error())
}
return
}
}
if e, ok := err.(*corbierror.Error); ok {
httpErr, status := corbierror.HTTPError(*e)
err := c.JSON(status, httpErr)
if err != nil {
logger.Error(err.Error())
}
return
}
err = c.JSON(500, corbierror.New("Internal error", corbierror.Internal, true))
if err != nil {
logger.Error(err.Error())
}
}
}