-
Notifications
You must be signed in to change notification settings - Fork 63
/
error_handler.go
135 lines (117 loc) · 3.78 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package router
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"allaboutapps.dev/aw/go-starter/internal/api/httperrors"
"allaboutapps.dev/aw/go-starter/internal/types"
"allaboutapps.dev/aw/go-starter/internal/util"
"github.com/go-openapi/swag"
"github.com/labstack/echo/v4"
)
var (
DefaultHTTPErrorHandlerConfig = HTTPErrorHandlerConfig{
HideInternalServerErrorDetails: true,
}
)
type HTTPErrorHandlerConfig struct {
HideInternalServerErrorDetails bool
}
func HTTPErrorHandler() echo.HTTPErrorHandler {
return HTTPErrorHandlerWithConfig(DefaultHTTPErrorHandlerConfig)
}
func HTTPErrorHandlerWithConfig(config HTTPErrorHandlerConfig) echo.HTTPErrorHandler {
return func(err error, c echo.Context) {
var code int64
var he error
var httpError *httperrors.HTTPError
var httpValidationError *httperrors.HTTPValidationError
var echoHTTPError *echo.HTTPError
switch {
case errors.As(err, &httpError):
code = *httpError.Code
he = httpError
if code == http.StatusInternalServerError && config.HideInternalServerErrorDetails {
if httpError.Internal == nil {
//nolint:errorlint
httpError.Internal = fmt.Errorf("Internal Error: %s", httpError)
}
httpError.Title = swag.String(http.StatusText(http.StatusInternalServerError))
}
case errors.As(err, &httpValidationError):
code = *httpValidationError.Code
he = httpValidationError
if code == http.StatusInternalServerError && config.HideInternalServerErrorDetails {
if httpValidationError.Internal == nil {
//nolint:errorlint
httpValidationError.Internal = fmt.Errorf("Internal Error: %s", httpValidationError)
}
httpValidationError.Title = swag.String(http.StatusText(http.StatusInternalServerError))
}
case errors.As(err, &echoHTTPError):
code = int64(echoHTTPError.Code)
if code == http.StatusInternalServerError && config.HideInternalServerErrorDetails {
if echoHTTPError.Internal == nil {
//nolint:errorlint
echoHTTPError.Internal = fmt.Errorf("Internal Error: %s", echoHTTPError)
}
he = &httperrors.HTTPError{
PublicHTTPError: types.PublicHTTPError{
Code: swag.Int64(int64(echoHTTPError.Code)),
Title: swag.String(http.StatusText(http.StatusInternalServerError)),
Type: swag.String(httperrors.HTTPErrorTypeGeneric),
},
Internal: echoHTTPError.Internal,
}
} else {
msg, ok := echoHTTPError.Message.(string)
if !ok {
if m, errr := json.Marshal(msg); err == nil {
msg = string(m)
} else {
msg = fmt.Sprintf("failed to marshal HTTP error message: %v", errr)
}
}
he = &httperrors.HTTPError{
PublicHTTPError: types.PublicHTTPError{
Code: swag.Int64(int64(echoHTTPError.Code)),
Title: &msg,
Type: swag.String(httperrors.HTTPErrorTypeGeneric),
},
Internal: echoHTTPError.Internal,
}
}
default:
code = http.StatusInternalServerError
if config.HideInternalServerErrorDetails {
he = &httperrors.HTTPError{
PublicHTTPError: types.PublicHTTPError{
Code: swag.Int64(int64(http.StatusInternalServerError)),
Title: swag.String(http.StatusText(http.StatusInternalServerError)),
Type: swag.String(httperrors.HTTPErrorTypeGeneric),
},
Internal: err,
}
} else {
he = &httperrors.HTTPError{
PublicHTTPError: types.PublicHTTPError{
Code: swag.Int64(int64(http.StatusInternalServerError)),
Title: swag.String(err.Error()),
Type: swag.String(httperrors.HTTPErrorTypeGeneric),
},
}
}
}
if !c.Response().Committed {
if c.Request().Method == http.MethodHead {
err = c.NoContent(int(code))
} else {
err = c.JSON(int(code), he)
}
if err != nil {
util.LogFromEchoContext(c).Warn().Err(err).AnErr("http_err", err).Msg("Failed to handle HTTP error")
}
}
}
}