forked from bugsnag/bugsnag-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bugsnagmiddleware.go
67 lines (54 loc) · 1.73 KB
/
bugsnagmiddleware.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Package bugsnagmartini provides a martini middleware that sends
panics to Bugsnag. You should use this middleware in combination
with martini.Recover() if you want to send error messages to your
clients:
func main() {
m := martini.New()
// used to stop panics bubbling and return a 500 error.
m.Use(martini.Recovery())
// used to send panics to Bugsnag.
m.Use(bugsnagmartini.AutoNotify(bugsnag.Configuration{
APIKey: "YOUR_API_KEY_HERE",
})
// ...
}
This middleware also makes bugsnag available to martini handlers via
the context.
func myHandler(w http.ResponseWriter, r *http.Request, bugsnag *bugsnag.Notifier) {
// ...
bugsnag.Notify(err)
// ...
}
*/
package bugsnagmartini
import (
"net/http"
"github.com/bugsnag/bugsnag-go"
"github.com/go-martini/martini"
)
const FrameworkName string = "Martini"
// AutoNotify sends any panics to bugsnag, and then re-raises them.
// You should use this after another middleware that
// returns an error page to the client, for example martini.Recover().
// The arguments can be any RawData to pass to Bugsnag, most usually
// you'll pass a bugsnag.Configuration object.
func AutoNotify(rawData ...interface{}) martini.Handler {
state := bugsnag.HandledState{
bugsnag.SeverityReasonUnhandledMiddlewareError,
bugsnag.SeverityError,
true,
FrameworkName,
}
rawData = append(rawData, state)
// set the release stage based on the martini environment.
rawData = append([]interface{}{bugsnag.Configuration{ReleaseStage: martini.Env}},
rawData...)
return func(r *http.Request, c martini.Context) {
// create a notifier that has the current request bound to it
notifier := bugsnag.New(append(rawData, r)...)
defer notifier.AutoNotify(r)
c.Map(notifier)
c.Next()
}
}