-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy patherror.go
112 lines (98 loc) · 3.23 KB
/
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
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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package web
import (
"context"
"errors"
"net/http"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/bangumi/server/internal/pkg/logger"
"github.com/bangumi/server/web/req/cf"
"github.com/bangumi/server/web/res"
"github.com/bangumi/server/web/util"
)
func globalNotFoundHandler(c echo.Context) error {
return c.JSON(http.StatusNotFound, res.Error{
Title: "Not Found",
Description: "This is default response, if you see this response, please check your request",
Details: util.Detail(c),
})
}
//nolint:funlen
func getDefaultErrorHandler() echo.HTTPErrorHandler {
var log = logger.Named("http.err").
WithOptions(zap.AddStacktrace(zapcore.PanicLevel), zap.WithCaller(false))
return func(err error, c echo.Context) {
reqID := c.Request().Header.Get(cf.HeaderRequestID)
{
var e res.HTTPError
if errors.As(err, &e) {
// handle expected http error
_ = c.JSON(e.Code, res.Error{
Title: http.StatusText(e.Code),
Description: e.Msg,
RequestID: reqID,
Details: util.Detail(c),
})
return
}
}
{
//nolint:forbidigo,errorlint
if e, ok := err.(*echo.HTTPError); ok {
log.Error("unexpected echo error",
zap.Int("code", e.Code),
zap.Any("message", e.Message),
zap.String("request_method", c.Request().Method),
zap.String("request_uri", c.Request().URL.Path),
zap.String("request_query", c.Request().URL.RawQuery),
zap.String("request_id", reqID),
)
_ = c.JSON(http.StatusInternalServerError, res.Error{
Title: http.StatusText(e.Code),
Description: e.Error(),
RequestID: reqID,
Details: util.DetailWithErr(c, err),
})
return
}
}
if errors.Is(err, context.Canceled) {
log.Error("request timeout",
zap.String("message", err.Error()),
zap.String("request_method", c.Request().Method),
zap.String("request_uri", c.Request().URL.Path),
zap.String("request_query", c.Request().URL.RawQuery),
zap.String("request_id", reqID),
)
_ = c.JSON(http.StatusInternalServerError, res.Error{
Title: "request timeout",
Description: "request timeout",
RequestID: c.Request().Header.Get(cf.HeaderRequestID),
})
return
}
log.Error("unexpected error",
zap.Error(err),
zap.String("request_method", c.Request().Method),
zap.String("request_uri", c.Request().URL.Path),
zap.String("request_query", c.Request().URL.RawQuery),
zap.String("request_id", reqID),
)
// unexpected error, return internal server error
_ = res.InternalError(c, err, "Unexpected Internal Server Error")
}
}