Move panic handling out of router

This commit is contained in:
DarthSim
2022-01-13 22:36:06 +06:00
parent b7cc1cb3a8
commit 86b646fe1b
2 changed files with 27 additions and 28 deletions

View File

@@ -19,7 +19,6 @@ var (
)
type RouteHandler func(string, http.ResponseWriter, *http.Request)
type PanicHandler func(string, http.ResponseWriter, *http.Request, error)
type route struct {
Method string
@@ -29,9 +28,8 @@ type route struct {
}
type Router struct {
prefix string
Routes []*route
PanicHandler PanicHandler
prefix string
Routes []*route
}
func (r *route) isMatch(req *http.Request) bool {
@@ -95,16 +93,6 @@ func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
replaceRemoteAddr(req, ip)
}
defer func() {
if rerr := recover(); rerr != nil {
if err, ok := rerr.(error); ok && r.PanicHandler != nil {
r.PanicHandler(reqID, rw, req, err)
} else {
panic(rerr)
}
}
}()
LogRequest(reqID, req)
for _, rr := range r.Routes {