aboutsummaryrefslogtreecommitdiff
path: root/core/middleware.go
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2023-10-24 07:20:10 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2023-10-24 07:20:29 +0200
commit18476c6cba2d5d2bb3ecf3ad136dacbf41d0360e (patch)
treea7a958ff7a4bf14ed7f7ee3ffd9c84b7592facbd /core/middleware.go
feat: Extract from other repo
Diffstat (limited to 'core/middleware.go')
-rw-r--r--core/middleware.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/core/middleware.go b/core/middleware.go
new file mode 100644
index 0000000..55e67fb
--- /dev/null
+++ b/core/middleware.go
@@ -0,0 +1,31 @@
+package core
+
+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)
+}