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

Commit

Permalink
http: add default recovery Mux
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Jun 29, 2015
1 parent 35d02e9 commit 0138232
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions http.go
@@ -1,9 +1,12 @@
package raven

import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
"runtime/debug"
"strings"
)

Expand Down Expand Up @@ -58,3 +61,24 @@ type Http struct {
}

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

// Recovery handler to wrap the stdlib net/http Mux.
// Example:
// http.HandleFunc("/", raven.RecoveryHandler(func(w http.ResponseWriter, r *http.Request) {
// ...
// }))
func RecoveryHandler(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := NewPacket(rvalStr, NewException(errors.New(rvalStr), NewStacktrace(2, 3, nil)), NewHttp(r))
Capture(packet, nil)
w.WriteHeader(http.StatusInternalServerError)
}
}()

handler(w, r)
}
}

0 comments on commit 0138232

Please sign in to comment.