-
Notifications
You must be signed in to change notification settings - Fork 2
/
error_handler.go
81 lines (73 loc) · 2.13 KB
/
error_handler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package echo
import (
"fmt"
"net/http"
"strconv"
"github.com/americanas-go/errors"
response "github.com/americanas-go/rest-response"
"github.com/go-playground/validator/v10"
e "github.com/labstack/echo/v4"
)
// ErrorHandlerString implements plain text content type error handler.
func ErrorHandlerString(err error, c e.Context) {
errorHandler(err, c, e.MIMETextPlain)
}
// ErrorHandlerJSON implements JSON content type error handler.
func ErrorHandlerJSON(err error, c e.Context) {
errorHandler(err, c, e.MIMEApplicationJSON)
}
func errorHandler(err error, c e.Context, contentType string) {
var (
status int
message string
)
if echoErr, ok := err.(*e.HTTPError); ok {
status = echoErr.Code
message = fmt.Sprintf("%v", echoErr.Message)
} else {
status = ErrorStatusCode(err)
message = err.Error()
}
var er error
if c.Request().Method == http.MethodHead {
er = c.NoContent(status)
} else {
switch contentType {
case e.MIMEApplicationJSON:
er = c.JSON(status, response.Error{HttpStatusCode: status, ErrorCode: strconv.Itoa(status), Message: message})
default:
er = c.String(status, message)
}
}
if er != nil {
c.Logger().Error(er)
}
}
// ErrorStatusCode translates to the respective status code.
func ErrorStatusCode(err error) int {
switch {
case errors.IsNotFound(err):
return http.StatusNotFound
case errors.IsMethodNotAllowed(err):
return http.StatusMethodNotAllowed
case errors.IsNotValid(err) || errors.IsBadRequest(err):
return http.StatusBadRequest
case errors.IsServiceUnavailable(err):
return http.StatusServiceUnavailable
case errors.IsConflict(err) || errors.IsAlreadyExists(err):
return http.StatusConflict
case errors.IsNotImplemented(err) || errors.IsNotProvisioned(err):
return http.StatusNotImplemented
case errors.IsUnauthorized(err):
return http.StatusUnauthorized
case errors.IsForbidden(err):
return http.StatusForbidden
case errors.IsNotSupported(err) || errors.IsNotAssigned(err):
return http.StatusUnprocessableEntity
default:
if _, ok := err.(validator.ValidationErrors); ok {
return http.StatusUnprocessableEntity
}
return http.StatusInternalServerError
}
}