forked from bugsnag/bugsnag-go
/
main.go
58 lines (46 loc) · 1.5 KB
/
main.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
package main
import (
"fmt"
"net/http"
"os"
"github.com/bugsnag/bugsnag-go"
"github.com/bugsnag/bugsnag-go/negroni"
"github.com/urfave/negroni"
)
// Insert your API key
const apiKey = "YOUR API KEY"
func main() {
if len(apiKey) != 32 {
fmt.Println("Please set your API key in main.go before running example.")
return
}
bugsnag.Configure(bugsnag.Configuration{APIKey: apiKey})
mux := http.NewServeMux()
mux.HandleFunc("/unhandled", unhandledCrash)
mux.HandleFunc("/handled", handledError)
n := negroni.New()
n.Use(negroni.NewRecovery())
// Add bugsnag handler after negroni.NewRecovery() to ensure panics get picked up
n.Use(bugsnagnegroni.AutoNotify())
n.UseHandler(mux)
fmt.Println("=============================================================================")
fmt.Println("Visit http://localhost:9001/unhandled - To perform an unhandled crash")
fmt.Println("Visit http://localhost:9001/handled - To create a manual error notification")
fmt.Println("=============================================================================")
fmt.Println("")
http.ListenAndServe(":9001", n)
}
func unhandledCrash(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("OK\n"))
// Invalid type assertion, will panic
func(a interface{}) string { return a.(string) }(struct{}{})
}
func handledError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("OK\n"))
_, err := os.Open("nonexistent_file.txt")
if err != nil {
bugsnag.Notify(err, r.Context())
}
}