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 ( import (
// commands // commands
"github.com/apex/log"
_ "github.com/ooni/probe-cli/internal/cli/geoip" _ "github.com/ooni/probe-cli/internal/cli/geoip"
_ "github.com/ooni/probe-cli/internal/cli/info" _ "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/show"
_ "github.com/ooni/probe-cli/internal/cli/upload" _ "github.com/ooni/probe-cli/internal/cli/upload"
_ "github.com/ooni/probe-cli/internal/cli/version" _ "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/cli/app"
"github.com/ooni/probe-cli/internal/crashreport"
) )
func main() { func main() {
crashreport.CapturePanicAndWait(func() { crashreport.CapturePanicAndWait(app.Run, nil)
err := app.Run()
if err == nil {
return
}
log.WithError(err).Fatal("main exit")
}, nil)
} }

View File

@ -3,13 +3,17 @@ package app
import ( import (
"os" "os"
"github.com/apex/log"
ooni "github.com/ooni/probe-cli" ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/cli/root" "github.com/ooni/probe-cli/internal/cli/root"
) )
// Run the app. This is the main app entry point // Run the app. This is the main app entry point
func Run() error { func Run() {
root.Cmd.Version(ooni.Version) root.Cmd.Version(ooni.Version)
_, err := root.Cmd.Parse(os.Args[1:]) _, 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 ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"reflect"
"time" "time"
"github.com/apex/log" "github.com/apex/log"
@ -221,16 +222,29 @@ func CreateOrUpdateURL(sess sqlbuilder.Database, url string, categoryCode string
// AddTestKeys writes the summary to the measurement // AddTestKeys writes the summary to the measurement
func AddTestKeys(sess sqlbuilder.Database, msmt *Measurement, tk interface{}) error { func AddTestKeys(sess sqlbuilder.Database, msmt *Measurement, tk interface{}) error {
var (
isAnomaly bool
isAnomalyValid bool
)
tkBytes, err := json.Marshal(tk) tkBytes, err := json.Marshal(tk)
if err != nil { if err != nil {
log.WithError(err).Error("failed to serialize summary") 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.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) err = sess.Collection("measurements").Find("id", msmt.ID).Update(msmt)
if err != nil { if err != nil {
log.WithError(err).Error("failed to update measurement")
return errors.Wrap(err, "updating measurement") return errors.Wrap(err, "updating measurement")
} }
return nil return nil

View File

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