diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 08:38:40 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2023-10-24 08:38:40 +0200 |
| commit | a9927763f43d59c418b264790ccee59defe8ca29 (patch) | |
| tree | 5dfc4f1f1f29d1ef1cbca3406bf57d1667b93f76 /kcore/middleware.go | |
| parent | ed23d402fec268e163ec895748f43e3254ba3dfd (diff) | |
refactor: Rename packages for clarityv0.3.0
Diffstat (limited to 'kcore/middleware.go')
| -rw-r--r-- | kcore/middleware.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/kcore/middleware.go b/kcore/middleware.go new file mode 100644 index 0000000..1537f55 --- /dev/null +++ b/kcore/middleware.go @@ -0,0 +1,31 @@ +package kcore + +import ( + "errors" + "net/http" + + "golang.org/x/exp/slog" +) + +func RecoverMiddleware(next http.Handler) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + defer func() { + if rcv := recover(); rcv != nil { + switch err := rcv.(type) { + case error: + if errors.Is(err, http.ErrAbortHandler) { + panic(rcv) + } + slog.Error(err.Error()) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + default: + panic(err) + } + } + }() + + next.ServeHTTP(w, r) + } + + return http.HandlerFunc(fn) +} |
