~mna/kick-archived-see-github

52ed383289797d7a349819d9ebbf3b72e594272f — Martin Angers 4 years ago 3ec59c2
add stack trace as argument to the panic recovery function
4 files changed, 9 insertions(+), 8 deletions(-)

M builder/builder_test.go
M builder/middleware.go
M builder/middleware_test.go
M kick.go
M builder/builder_test.go => builder/builder_test.go +4 -4
@@ 157,7 157,7 @@ func TestHandler_InvalidCanonicalRedirect(t *testing.T) {
		Root: &kick.Root{
			MethodNotAllowedHandler:         statusHandler(405),
			NotFoundHandler:                 statusHandler(404),
			PanicRecoveryFunc:               func(w http.ResponseWriter, r *http.Request, v interface{}) {},
			PanicRecoveryFunc:               func(w http.ResponseWriter, r *http.Request, v interface{}, stack []byte) {},
			TrustProxyHeaders:               true,
			AllowMethodOverride:             true,
			RequestIDHeader:                 "X",


@@ 305,7 305,7 @@ func TestHandler_Valid(t *testing.T) {
		Root: &kick.Root{
			MethodNotAllowedHandler: statusHandler(405),
			NotFoundHandler:         statusHandler(404),
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}) {
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}, stack []byte) {
				w.WriteHeader(500)
			},
			LoggingFunc: func(w http.ResponseWriter, r *http.Request, info map[string]interface{}) {


@@ 709,7 709,7 @@ func TestHandler_HEAD(t *testing.T) {

	s := &kick.Server{
		Root: &kick.Root{
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}) {
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}, stack []byte) {
				w.WriteHeader(500)
			},
			LoggingFunc: func(w http.ResponseWriter, r *http.Request, info map[string]interface{}) {


@@ 779,7 779,7 @@ func TestHandler_CSRF(t *testing.T) {
	}
	s := &kick.Server{
		Root: &kick.Root{
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}) {
			PanicRecoveryFunc: func(w http.ResponseWriter, r *http.Request, v interface{}, stack []byte) {
				w.WriteHeader(500)
			},
			LoggingFunc: func(w http.ResponseWriter, r *http.Request, info map[string]interface{}) {

M builder/middleware.go => builder/middleware.go +3 -2
@@ 5,6 5,7 @@ import (
	"encoding/hex"
	"fmt"
	"net/http"
	"runtime/debug"
	"strconv"
	"time"



@@ 177,12 178,12 @@ func timeoutHandler(timeout time.Duration) func(http.Handler) http.Handler {

// panicRecovery calls the wrapped handler and recovers from a
// panic, calling recoverFn to handle the response to the failed request.
func panicRecovery(recoverFn func(http.ResponseWriter, *http.Request, interface{})) func(http.Handler) http.Handler {
func panicRecovery(recoverFn func(http.ResponseWriter, *http.Request, interface{}, []byte)) func(http.Handler) http.Handler {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			defer func() {
				if err := recover(); err != nil {
					recoverFn(w, r, err)
					recoverFn(w, r, err, debug.Stack())
				}
			}()
			h.ServeHTTP(w, r)

M builder/middleware_test.go => builder/middleware_test.go +1 -1
@@ 235,7 235,7 @@ func TestTimeoutHandler(t *testing.T) {
}

func TestPanicRecovery(t *testing.T) {
	recoverFn := func(w http.ResponseWriter, r *http.Request, v interface{}) {
	recoverFn := func(w http.ResponseWriter, r *http.Request, v interface{}, stack []byte) {
		if v != io.EOF {
			t.Fatalf("want panic value %v, got %v", io.EOF, v)
		}

M kick.go => kick.go +1 -1
@@ 380,7 380,7 @@ type Root struct {
	// already recovered when this is called, and the recovered
	// value is passed as panicVal. If nil, there is no panic
	// recovery.
	PanicRecoveryFunc func(w http.ResponseWriter, r *http.Request, panicVal interface{})
	PanicRecoveryFunc func(w http.ResponseWriter, r *http.Request, panicVal interface{}, stackTrace []byte)

	// LoggingFunc is the function called to log a request.
	// Relevant information is stored in the info map.