-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbugsnaggin.go
45 lines (40 loc) · 1.36 KB
/
bugsnaggin.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
package bugsnaggin
import (
"github.com/bugsnag/bugsnag-go/v2"
"github.com/bugsnag/bugsnag-go/v2/device"
"github.com/gin-gonic/gin"
)
const frameworkName string = "Gin"
// 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 gin.Recovery().
// The arguments can be any RawData to pass to Bugsnag, most usually
// you'll pass a bugsnag.Configuration object.
func AutoNotify(rawData ...interface{}) gin.HandlerFunc {
// Configure bugsnag with the passed in configuration (for manual notifications)
for _, datum := range rawData {
if c, ok := datum.(bugsnag.Configuration); ok {
bugsnag.Configure(c)
}
}
device.AddVersion(frameworkName, gin.Version)
state := bugsnag.HandledState{
SeverityReason: bugsnag.SeverityReasonUnhandledMiddlewareError,
OriginalSeverity: bugsnag.SeverityError,
Unhandled: true,
Framework: frameworkName,
}
rawData = append(rawData, state)
return func(c *gin.Context) {
r := c.Copy().Request
notifier := bugsnag.New(append(rawData, r)...)
ctx := bugsnag.AttachRequestData(r.Context(), r)
if notifier.Config.IsAutoCaptureSessions() {
ctx = bugsnag.StartSession(ctx)
}
c.Request = r.WithContext(ctx)
notifier.FlushSessionsOnRepanic(false)
defer notifier.AutoNotify(ctx)
c.Next()
}
}