@@ -16,7 +16,6 @@ import (
1616 "io/ioutil"
1717
1818 "github.com/bugsnag/bugsnag-go"
19- "github.com/docker/machine/commands/mcndirs"
2019 "github.com/docker/machine/libmachine/log"
2120 "github.com/docker/machine/libmachine/mcnutils"
2221 "github.com/docker/machine/version"
@@ -27,28 +26,53 @@ const (
2726 noreportAPIKey = "no-report"
2827)
2928
30- var apiKey string
29+ type CrashReporter interface {
30+ Send (err CrashError ) error
31+ }
32+
33+ // CrashError describes an error that should be reported to bugsnag
34+ type CrashError struct {
35+ Cause error
36+ Command string
37+ Context string
38+ DriverName string
39+ LogFilePath string
40+ }
3141
32- // Configure the apikey for bugnag
33- func Configure (key string ) {
34- apiKey = defaultAPIKey
35- if key != "" {
36- apiKey = key
42+ func (e CrashError ) Error () string {
43+ return e .Cause .Error ()
44+ }
45+
46+ type BugsnagCrashReporter struct {
47+ baseDir string
48+ apiKey string
49+ }
50+
51+ // NewCrashReporter creates a new bugsnag based CrashReporter. Needs an apiKey.
52+ func NewCrashReporter (baseDir string , apiKey string ) * BugsnagCrashReporter {
53+ if apiKey == "" {
54+ apiKey = defaultAPIKey
55+ }
56+
57+ return & BugsnagCrashReporter {
58+ baseDir : baseDir ,
59+ apiKey : apiKey ,
3760 }
3861}
3962
40- func SendWithFile (err error , context string , driverName string , command string , path string ) error {
41- if noReportFileExist () || apiKey == noreportAPIKey {
63+ // Send sends a crash report to bugsnag via an http call.
64+ func (r * BugsnagCrashReporter ) Send (err CrashError ) error {
65+ if r .noReportFileExist () || r .apiKey == noreportAPIKey {
4266 log .Debug ("Opting out of crash reporting." )
4367 return nil
4468 }
4569
46- if apiKey == "" {
70+ if r . apiKey == "" {
4771 return errors .New ("Not sending report since no api key has been set." )
4872 }
4973
5074 bugsnag .Configure (bugsnag.Configuration {
51- APIKey : apiKey ,
75+ APIKey : r . apiKey ,
5276 // XXX we need to abuse bugsnag metrics to get the OS/ARCH information as a usable filter
5377 // Can do that with either "stage" or "hostname"
5478 ReleaseStage : fmt .Sprintf ("%s (%s)" , runtime .GOOS , runtime .GOARCH ),
@@ -68,19 +92,23 @@ func SendWithFile(err error, context string, driverName string, command string,
6892 detectRunningShell (& metaData )
6993 detectUname (& metaData )
7094 detectOSVersion (& metaData )
71- addFile (path , & metaData )
95+ addFile (err . LogFilePath , & metaData )
7296
7397 var buffer bytes.Buffer
7498 for _ , message := range log .History () {
7599 buffer .WriteString (message + "\n " )
76100 }
77101 metaData .Add ("history" , "trace" , buffer .String ())
78- return bugsnag .Notify (err , metaData , bugsnag .SeverityError , bugsnag.Context {String : context }, bugsnag.ErrorClass {Name : fmt .Sprintf ("%s/%s" , driverName , command )})
102+
103+ return bugsnag .Notify (err .Cause , metaData , bugsnag .SeverityError , bugsnag.Context {String : err .Context }, bugsnag.ErrorClass {Name : fmt .Sprintf ("%s/%s" , err .DriverName , err .Command )})
79104}
80105
81- // Send through http the crash report to bugsnag need a call to Configure(apiKey) before
82- func Send (err error , context string , driverName string , command string ) error {
83- return SendWithFile (err , context , driverName , command , "" )
106+ func (r * BugsnagCrashReporter ) noReportFileExist () bool {
107+ optOutFilePath := filepath .Join (r .baseDir , "no-error-report" )
108+ if _ , err := os .Stat (optOutFilePath ); os .IsNotExist (err ) {
109+ return false
110+ }
111+ return true
84112}
85113
86114func addFile (path string , metaData * bugsnag.MetaData ) {
@@ -97,14 +125,6 @@ func addFile(path string, metaData *bugsnag.MetaData) {
97125 metaData .Add ("logfile" , filepath .Base (path ), string (data ))
98126}
99127
100- func noReportFileExist () bool {
101- optOutFilePath := filepath .Join (mcndirs .GetBaseDir (), "no-error-report" )
102- if _ , err := os .Stat (optOutFilePath ); os .IsNotExist (err ) {
103- return false
104- }
105- return true
106- }
107-
108128func detectRunningShell (metaData * bugsnag.MetaData ) {
109129 shell := os .Getenv ("SHELL" )
110130 if shell != "" {
0 commit comments