forked from bugsnag/bugsnag-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bugsnag_example_test.go
159 lines (135 loc) · 3.82 KB
/
bugsnag_example_test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package bugsnag_test
import (
"context"
"fmt"
"net"
"net/http"
"time"
"github.com/bugsnag/bugsnag-go"
)
var exampleAPIKey = "166f5ad3590596f9aa8d601ea89af845"
func ExampleAutoNotify() {
bugsnag.Configure(bugsnag.Configuration{APIKey: exampleAPIKey})
createAccount := func(ctx context.Context) {
fmt.Println("Creating account and passing context around...")
}
ctx := bugsnag.StartSession(context.Background())
defer bugsnag.AutoNotify(ctx)
createAccount(ctx)
// Output:
// Creating account and passing context around...
}
func ExampleRecover() {
bugsnag.Configure(bugsnag.Configuration{APIKey: exampleAPIKey})
panicFunc := func() {
fmt.Println("About to panic")
panic("Oh noes")
}
// Will recover when panicFunc panics
func() {
ctx := bugsnag.StartSession(context.Background())
defer bugsnag.Recover(ctx)
panicFunc()
}()
fmt.Println("Panic recovered")
// Output: About to panic
// Panic recovered
}
func ExampleConfigure() {
bugsnag.Configure(bugsnag.Configuration{
APIKey: "YOUR_API_KEY_HERE",
ReleaseStage: "production",
// See bugsnag.Configuration for other fields
})
}
func ExampleHandler() {
handleReq := func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Handling HTTP request")
}
// Set up your http handlers as usual
http.HandleFunc("/", handleReq)
// use bugsnag.Handler(nil) to wrap the default http handlers
// so that Bugsnag is automatically notified about panics.
http.ListenAndServe(":1234", bugsnag.Handler(nil))
}
func ExampleHandler_customServer() {
handleReq := func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Handling GET")
}
// If you're using a custom server, set the handlers explicitly.
http.HandleFunc("/", handleReq)
srv := http.Server{
Addr: ":1234",
ReadTimeout: 10 * time.Second,
// use bugsnag.Handler(nil) to wrap the default http handlers
// so that Bugsnag is automatically notified about panics.
Handler: bugsnag.Handler(nil),
}
srv.ListenAndServe()
}
func ExampleHandler_customHandlers() {
handleReq := func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Handling GET")
}
// If you're using custom handlers, wrap the handlers explicitly.
handler := http.NewServeMux()
http.HandleFunc("/", handleReq)
// use bugsnag.Handler(handler) to wrap the handlers so that Bugsnag is
// automatically notified about panics
http.ListenAndServe(":1234", bugsnag.Handler(handler))
}
func ExampleNotify() {
ctx := context.Background()
ctx = bugsnag.StartSession(ctx)
_, err := net.Listen("tcp", ":80")
if err != nil {
bugsnag.Notify(err, ctx)
}
}
func ExampleNotify_details() {
ctx := context.Background()
ctx = bugsnag.StartSession(ctx)
_, err := net.Listen("tcp", ":80")
if err != nil {
bugsnag.Notify(err, ctx,
// show as low-severity
bugsnag.SeverityInfo,
// set the context
bugsnag.Context{String: "createlistener"},
// pass the user id in to count users affected.
bugsnag.User{Id: "123456789"},
// custom meta-data tab
bugsnag.MetaData{
"Listen": {
"Protocol": "tcp",
"Port": "80",
},
},
)
}
}
func ExampleOnBeforeNotify() {
type Job struct {
Retry bool
UserID string
UserEmail string
}
bugsnag.OnBeforeNotify(func(event *bugsnag.Event, config *bugsnag.Configuration) error {
// Search all the RawData for any *Job pointers that we're passed in
// to bugsnag.Notify() and friends.
for _, datum := range event.RawData {
if job, ok := datum.(*Job); ok {
// don't notify bugsnag about errors in retries
if job.Retry {
return fmt.Errorf("bugsnag middleware: not notifying about job retry")
}
// add the job as a tab on Bugsnag.com
event.MetaData.AddStruct("Job", job)
// set the user correctly
event.User = &bugsnag.User{Id: job.UserID, Email: job.UserEmail}
}
}
// continue notifying as normal
return nil
})
}