From 5bc63b7864da9c7161204ccdaad56dc7322fdadd Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Fri, 27 Aug 2021 08:47:24 -0700 Subject: [PATCH] Add fatal error dialog --- cmd/itgui/error.go | 21 ++++++++++++++++++--- cmd/itgui/info.go | 4 ++-- cmd/itgui/main.go | 7 +++++++ cmd/itgui/notify.go | 2 +- cmd/itgui/time.go | 2 +- cmd/itgui/upgrade.go | 10 +++++----- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/cmd/itgui/error.go b/cmd/itgui/error.go index ae47989..8cdd4db 100644 --- a/cmd/itgui/error.go +++ b/cmd/itgui/error.go @@ -2,6 +2,7 @@ package main import ( "image/color" + "os" "fyne.io/fyne/v2" "fyne.io/fyne/v2/canvas" @@ -10,7 +11,7 @@ import ( "fyne.io/fyne/v2/widget" ) -func guiErr(err error, msg string, parent fyne.Window) { +func guiErr(err error, msg string, fatal bool, parent fyne.Window) { // Create new label containing message msgLbl := widget.NewLabel(msg) // Text formatting settings @@ -33,6 +34,20 @@ func guiErr(err error, msg string, parent fyne.Window) { widget.NewAccordionItem("More Details", errLbl), )) } - // Show error dialog - dialog.NewCustom("Error", "Ok", content, parent).Show() + if fatal { + // Create new error dialog + errDlg := dialog.NewCustom("Error", "Close", content, parent) + // On close, exit with code 1 + errDlg.SetOnClosed(func() { + os.Exit(1) + }) + // Show dialog + errDlg.Show() + // Run app prematurely to stop further execution + parent.ShowAndRun() + } else { + // Show error dialog + dialog.NewCustom("Error", "Ok", content, parent).Show() + } + } diff --git a/cmd/itgui/info.go b/cmd/itgui/info.go index aec6ae3..cb082aa 100644 --- a/cmd/itgui/info.go +++ b/cmd/itgui/info.go @@ -58,7 +58,7 @@ func infoTab(parent fyne.Window) *fyne.Container { fwVerString, err := get(types.ReqTypeFwVersion) if err != nil { - panic(err) + guiErr(err, "Error getting firmware string", true, parent) } fwVer := container.NewVBox( @@ -99,7 +99,7 @@ func watch(req int, onRecv func(data interface{}), parent fyne.Window) error { for scanner.Scan() { res, err := getResp(scanner.Bytes()) if err != nil { - guiErr(err, "Error getting response from connection", parent) + guiErr(err, "Error getting response from connection", false, parent) continue } onRecv(res.Value) diff --git a/cmd/itgui/main.go b/cmd/itgui/main.go index 344d0e0..0bdf961 100644 --- a/cmd/itgui/main.go +++ b/cmd/itgui/main.go @@ -1,6 +1,8 @@ package main import ( + "net" + "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" ) @@ -13,6 +15,11 @@ func main() { // Create new window with title "itgui" window := a.NewWindow("itgui") + _, err := net.Dial("unix", SockPath) + if err != nil { + guiErr(err, "Error dialing itd socket", true, window) + } + // Create new app tabs container tabs := container.NewAppTabs( container.NewTabItem("Info", infoTab(window)), diff --git a/cmd/itgui/notify.go b/cmd/itgui/notify.go index 009160e..51f63d6 100644 --- a/cmd/itgui/notify.go +++ b/cmd/itgui/notify.go @@ -25,7 +25,7 @@ func notifyTab(parent fyne.Window) *fyne.Container { // Dial itd UNIX socket conn, err := net.Dial("unix", SockPath) if err != nil { - guiErr(err, "Error dialing socket", parent) + guiErr(err, "Error dialing socket", false, parent) return } // Encode notify request on connection diff --git a/cmd/itgui/time.go b/cmd/itgui/time.go index d857e4f..0fe3ac7 100644 --- a/cmd/itgui/time.go +++ b/cmd/itgui/time.go @@ -29,7 +29,7 @@ func timeTab(parent fyne.Window) *fyne.Container { // Parse time as RFC1123 string parsedTime, err := time.Parse(time.RFC1123, timeEntry.Text) if err != nil { - guiErr(err, "Error parsing time string", parent) + guiErr(err, "Error parsing time string", false, parent) return } // Set time to parsed time diff --git a/cmd/itgui/upgrade.go b/cmd/itgui/upgrade.go index 80ca90d..24e7e61 100644 --- a/cmd/itgui/upgrade.go +++ b/cmd/itgui/upgrade.go @@ -92,7 +92,7 @@ func upgradeTab(parent fyne.Window) *fyne.Container { // If archive path does not exist and both init packet and firmware paths // also do not exist, return error if archivePath == "" && (initPktPath == "" && fiwmarePath == "") { - guiErr(nil, "Upgrade requires archive or files selected", parent) + guiErr(nil, "Upgrade requires archive or files selected", false, parent) return } @@ -125,7 +125,7 @@ func upgradeTab(parent fyne.Window) *fyne.Container { // Dial itd UNIX socket conn, err := net.Dial("unix", SockPath) if err != nil { - guiErr(err, "Error dialing socket", parent) + guiErr(err, "Error dialing socket", false, parent) return } defer conn.Close() @@ -150,18 +150,18 @@ func upgradeTab(parent fyne.Window) *fyne.Container { // Decode scanned line into response struct err = json.Unmarshal(scanner.Bytes(), &res) if err != nil { - guiErr(err, "Error decoding response", parent) + guiErr(err, "Error decoding response", false, parent) return } if res.Error { - guiErr(err, "Error returned in response", parent) + guiErr(err, "Error returned in response", false, parent) return } var event types.DFUProgress // Decode response data into progress struct err = mapstructure.Decode(res.Value, &event) if err != nil { - guiErr(err, "Error decoding response value", parent) + guiErr(err, "Error decoding response value", false, parent) return } // If transfer finished, break