numerus/pkg/recover.go

30 lines
593 B
Go
Raw Normal View History

package pkg
import (
2023-01-22 19:37:34 +00:00
"fmt"
"log"
"net/http"
"runtime"
)
func Recoverer(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
if r == http.ErrAbortHandler {
2023-01-22 19:37:34 +00:00
panic(r)
}
err, ok := r.(error)
2023-01-22 19:37:34 +00:00
if !ok {
err = fmt.Errorf("%v", r)
}
2023-01-22 19:37:34 +00:00
stack := make([]byte, 4<<10)
length := runtime.Stack(stack, true)
log.Printf("PANIC - %v %s", err, stack[:length])
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2023-01-22 19:37:34 +00:00
}()
next.ServeHTTP(w, r)
})
}