Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Print the recovered value before dumping stack trace #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"net"
"net/http"
"net/url"
"os"
"runtime/debug"
"strconv"
"strings"
)

Expand Down Expand Up @@ -62,6 +64,28 @@ type Http struct {

func (h *Http) Class() string { return "request" }

// Prints an argument passed to panic.
// There's room for arbitrary complexity here, but we keep it
// simple and handle just a few important cases: int, string, and Stringer.
//
// Taken from runtime/error.go in the standard library (how it prints panics)
func printany(i interface{}) string {
switch v := i.(type) {
case nil:
return "nil"
case fmt.Stringer:
return v.String()
case error:
return v.Error()
case int:
return strconv.Itoa(v)
case string:
return v
default:
return fmt.Sprintf("%#v", v)
}
}

// Recovery handler to wrap the stdlib net/http Mux.
// Example:
// http.HandleFunc("/", raven.RecoveryHandler(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -71,6 +95,8 @@ func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
os.Stderr.WriteString("panic: ")
os.Stderr.WriteString(printany(rval) + "\n\n")
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := NewPacket(rvalStr, NewException(errors.New(rvalStr), NewStacktrace(2, 3, nil)), NewHttp(r))
Expand Down