forked from bugsnag/bugsnag-go
/
bugsnagnegroni.go
53 lines (45 loc) · 1.43 KB
/
bugsnagnegroni.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package bugsnagnegroni
import (
"net/http"
"github.com/bugsnag/bugsnag-go"
"github.com/bugsnag/bugsnag-go/device"
"github.com/urfave/negroni"
)
// FrameworkName is the name of the framework this middleware applies to
const FrameworkName string = "Negroni"
type handler struct {
rawData []interface{}
}
// AutoNotify sends any panics to bugsnag, and then re-raises them.
func AutoNotify(rawData ...interface{}) negroni.Handler {
updateGlobalConfig(rawData...)
device.AddVersion(FrameworkName, "unknown") // Negroni exposes no version prop.
state := bugsnag.HandledState{
SeverityReason: bugsnag.SeverityReasonUnhandledMiddlewareError,
OriginalSeverity: bugsnag.SeverityError,
Unhandled: true,
Framework: FrameworkName,
}
rawData = append(rawData, state)
return &handler{rawData: rawData}
}
func (h *handler) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
// Record a session if auto capture sessions is enabled
ctx := bugsnag.AttachRequestData(r.Context(), r)
if bugsnag.Config.IsAutoCaptureSessions() {
ctx = bugsnag.StartSession(ctx)
}
request := r.WithContext(ctx)
notifier := bugsnag.New(h.rawData...)
notifier.FlushSessionsOnRepanic(false)
defer notifier.AutoNotify(ctx)
next(rw, request)
}
func updateGlobalConfig(rawData ...interface{}) {
for i, datum := range rawData {
if c, ok := datum.(bugsnag.Configuration); ok {
bugsnag.Configure(c)
rawData[i] = nil
}
}
}