30 lines
602 B
Go
30 lines
602 B
Go
package pkg
|
|
|
|
import (
|
|
"net/http"
|
|
"runtime"
|
|
"log"
|
|
"fmt"
|
|
)
|
|
|
|
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 {
|
|
panic(r);
|
|
}
|
|
err, ok := r.(error)
|
|
if ! ok {
|
|
err = fmt.Errorf("%v", r);
|
|
}
|
|
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)
|
|
}
|
|
}();
|
|
next.ServeHTTP(w, r);
|
|
});
|
|
}
|