41 lines
731 B
Go
Raw Normal View History

2019-06-01 04:55:49 +00:00
package middleware
import (
"fmt"
"net/http"
2019-06-03 05:53:04 +00:00
"os"
2019-06-01 04:55:49 +00:00
"runtime"
2019-06-03 05:53:04 +00:00
"strings"
2019-06-01 04:55:49 +00:00
"github.com/aerogo/aero"
)
// Recover recovers from panics and shows them as the response body.
func Recover(next aero.Handler) aero.Handler {
return func(ctx aero.Context) error {
defer func() {
r := recover()
if r == nil {
return
}
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
stack := make([]byte, 4096)
length := runtime.Stack(stack, true)
2019-06-03 05:53:04 +00:00
stackString := string(stack[:length])
fmt.Fprint(os.Stderr, stackString)
message := err.Error() + "<br><br>" + strings.ReplaceAll(stackString, "\n", "<br>")
_ = ctx.Error(http.StatusInternalServerError, message)
2019-06-01 04:55:49 +00:00
}()
return next(ctx)
}
}