1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-07-22 15:18:02 +00:00

Improved error logging and dialogs

This commit is contained in:
2023-06-24 20:27:48 +02:00
parent b73b5f4485
commit cb89b5923f
18 changed files with 444 additions and 208 deletions

View File

@ -6,7 +6,6 @@ import (
"os"
"path"
"github.com/ncruces/zenity"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
@ -34,20 +33,20 @@ func (a *App) Settings() Settings {
if err != nil {
// It's ok if the file cannot be opened, for example if it is not accessible.
// Therefore no error is returned.
runtime.LogInfo(a.ctx, "Cannot open settings.json:")
runtime.LogInfo(a.ctx, err.Error())
runtime.LogInfof(a.ctx, "Cannot open settings.json: %s", err.Error())
return s
}
if len(jsonData) == 0 {
return s
} else {
err = json.Unmarshal(jsonData, &s)
if err != nil {
runtime.LogWarning(a.ctx, "Cannot unmarshal settings.json:")
runtime.LogWarning(a.ctx, err.Error())
zenity.Warning("Could not retrieve application settings, using defaults!", zenity.WarningIcon)
if err := json.Unmarshal(jsonData, &s); err != nil {
runtime.LogWarningf(a.ctx, "Cannot unmarshal settings.json: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Settings malformed",
Message: "Could not retrieve application settings: using defaults!",
Type: runtime.WarningDialog,
})
}
return s
}
@ -57,26 +56,35 @@ func (a *App) UpdateSettings(jsonData string) Settings {
s := a.Settings()
err := json.Unmarshal([]byte(jsonData), &s)
if err != nil {
runtime.LogError(a.ctx, "Malformed JSON for settings file:")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Malformed JSON"), zenity.ErrorIcon)
runtime.LogErrorf(a.ctx, "Malformed JSON for settings file: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Settings malformed",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return s
}
newJson, err := json.MarshalIndent(s, "", "\t")
if err != nil {
runtime.LogError(a.ctx, "Could not marshal settings into JSON:")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Malformed JSON"), zenity.ErrorIcon)
runtime.LogErrorf(a.ctx, "Could not marshal settings into JSON: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "JSON is being awkward",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
return s
}
filePath := path.Join(a.Env.DataDirectory, "settings.json")
err = ioutil.WriteFile(filePath, newJson, os.ModePerm)
if err != nil {
runtime.LogError(a.ctx, "Could not update host list:")
runtime.LogError(a.ctx, err.Error())
zenity.Error(err.Error(), zenity.Title("Could not update host list"), zenity.ErrorIcon)
runtime.LogErrorf(a.ctx, "Could not update host list: %s", err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Could not update host list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
}
return s