/
main.go
47 lines (36 loc) · 1.21 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/bugsnag/bugsnag-go/v2"
)
// Insert your API key
const apiKey = "YOUR-API-KEY-HERE"
func main() {
if len(apiKey) != 32 {
fmt.Println("Please set the API key in main.go before running the example")
return
}
bugsnag.Configure(bugsnag.Configuration{APIKey: apiKey})
http.HandleFunc("/unhandled", unhandledCrash)
http.HandleFunc("/handled", handledError)
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", bugsnag.Handler(nil))
}
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) {
_, err := os.Open("nonexistent_file.txt")
if err != nil {
bugsnag.Notify(err, r.Context())
}
}