Make error reporting more robust to panic

This commit is contained in:
Arturo Filastò 2018-09-10 16:29:14 +02:00
parent 21e3a08b0e
commit e0c0acffeb
4 changed files with 30 additions and 15 deletions

View File

@ -2,7 +2,6 @@ package main
import (
// commands
"github.com/apex/log"
_ "github.com/ooni/probe-cli/internal/cli/geoip"
_ "github.com/ooni/probe-cli/internal/cli/info"
@ -14,17 +13,11 @@ import (
_ "github.com/ooni/probe-cli/internal/cli/show"
_ "github.com/ooni/probe-cli/internal/cli/upload"
_ "github.com/ooni/probe-cli/internal/cli/version"
"github.com/ooni/probe-cli/internal/crashreport"
"github.com/ooni/probe-cli/internal/cli/app"
"github.com/ooni/probe-cli/internal/crashreport"
)
func main() {
crashreport.CapturePanicAndWait(func() {
err := app.Run()
if err == nil {
return
}
log.WithError(err).Fatal("main exit")
}, nil)
crashreport.CapturePanicAndWait(app.Run, nil)
}

View File

@ -3,13 +3,17 @@ package app
import (
"os"
"github.com/apex/log"
ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/cli/root"
)
// Run the app. This is the main app entry point
func Run() error {
func Run() {
root.Cmd.Version(ooni.Version)
_, err := root.Cmd.Parse(os.Args[1:])
return err
if err != nil {
log.WithError(err).Error("failed to parse arguments")
}
return
}

View File

@ -3,6 +3,7 @@ package database
import (
"database/sql"
"encoding/json"
"reflect"
"time"
"github.com/apex/log"
@ -221,16 +222,29 @@ func CreateOrUpdateURL(sess sqlbuilder.Database, url string, categoryCode string
// AddTestKeys writes the summary to the measurement
func AddTestKeys(sess sqlbuilder.Database, msmt *Measurement, tk interface{}) error {
var (
isAnomaly bool
isAnomalyValid bool
)
tkBytes, err := json.Marshal(tk)
if err != nil {
log.WithError(err).Error("failed to serialize summary")
}
isAnomaly := tk.(struct{ IsAnomaly bool }).IsAnomaly
// This is necessary so that we can extract from the the opaque testKeys just
// the IsAnomaly field of bool type.
// Maybe generics are not so bad after-all, heh golang?
isAnomalyValue := reflect.ValueOf(tk).FieldByName("IsAnomaly")
if isAnomalyValue.IsValid() == true && isAnomalyValue.Kind() == reflect.Bool {
isAnomaly = isAnomalyValue.Bool()
isAnomalyValid = true
}
msmt.TestKeys = string(tkBytes)
msmt.IsAnomaly = sql.NullBool{Bool: isAnomaly, Valid: true}
msmt.IsAnomaly = sql.NullBool{Bool: isAnomaly, Valid: isAnomalyValid}
err = sess.Collection("measurements").Find("id", msmt.ID).Update(msmt)
if err != nil {
log.WithError(err).Error("failed to update measurement")
return errors.Wrap(err, "updating measurement")
}
return nil

View File

@ -12,9 +12,11 @@ import (
"github.com/fatih/color"
"github.com/measurement-kit/go-measurement-kit"
ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/crashreport"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/output"
"github.com/ooni/probe-cli/utils"
"github.com/ooni/probe-cli/utils/strcase"
)
// Nettest interface. Every Nettest should implement this.
@ -70,7 +72,7 @@ func (c *Controller) Init(nt *mk.Nettest) error {
// These values are shared by every measurement
reportID := sql.NullString{String: "", Valid: false}
testName := nt.Name
testName := strcase.ToSnake(nt.Name)
resultID := c.res.ID
reportFilePath := c.msmtPath
@ -270,7 +272,9 @@ func (c *Controller) Init(nt *mk.Nettest) error {
nt.On("measurement", func(e mk.Event) {
log.Debugf("status.end")
crashreport.CapturePanicAndWait(func() {
c.OnEntry(e.Value.Idx, e.Value.JSONStr)
}, nil)
})
nt.On("status.end", func(e mk.Event) {