-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorhandler.go
More file actions
63 lines (54 loc) · 1.2 KB
/
errorhandler.go
File metadata and controls
63 lines (54 loc) · 1.2 KB
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
package webapp
import (
"errors"
"net/http"
)
// ErrorHandler is a centralized error handler.
type ErrorHandler func(Context, error) error
var DefaultErrorHandler = func(c Context, err error) error {
if IsBindError(err) {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": err.Error(),
"error": errors.Unwrap(err),
"type": "bind",
})
}
if IsValidationError(err) {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": err.Error(),
"error": err,
"type": "validation",
})
}
he, ok := err.(*HTTPError)
if ok {
if he.Internal != nil {
if herr, ok := he.Internal.(*HTTPError); ok {
he = herr
}
}
} else {
he = &HTTPError{
Code: http.StatusInternalServerError,
Message: http.StatusText(http.StatusInternalServerError),
}
}
message := he.Message
if m, ok := he.Message.(string); ok {
//if e.Debug {
// message = Map{"message": m, "error": err.Error()}
//} else {
message = Map{"message": m}
//}
}
// Send response
if c.Request().Method == http.MethodHead {
c.Response().WriteHeader(he.Code)
} else {
err = c.JSON(he.Code, message)
}
//if err != nil {
//e.Logger().Error(err)
//}
return err
}