-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
42 lines (33 loc) · 948 Bytes
/
middlewares.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
package api
import (
"net/http"
"runtime/debug"
"github.com/go-chi/chi/v5/middleware"
)
func RouteNotFoundHandler(w http.ResponseWriter, r *http.Request) {
err := NotFound("route not found", "ROUTE_NOT_FOUND")
err.Write(w)
}
func MethodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
err := MethodNotAllowed("method not allowed", "METHOD_NOT_ALLOWED")
err.Write(w)
}
func PanicHandler(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil && rvr != http.ErrAbortHandler {
logEntry := middleware.GetLogEntry(r)
if logEntry != nil {
logEntry.Panic(rvr, debug.Stack())
} else {
middleware.PrintPrettyStack(rvr)
}
w.WriteHeader(http.StatusInternalServerError)
err := InternalServerError("server error", "SERVER_ERROR")
err.Write(w)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}