From 50b87e024054a87ef93936389f33b0d89f9727e0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 1 Mar 2022 15:29:41 +0100 Subject: [PATCH] Display error notifications when config loading failed --- notifications/module.go | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/notifications/module.go b/notifications/module.go index 764035f..66d5ec3 100644 --- a/notifications/module.go +++ b/notifications/module.go @@ -1,15 +1,17 @@ package notifications import ( + "fmt" "time" + "github.com/safing/portbase/config" "github.com/safing/portbase/modules" ) var module *modules.Module func init() { - module = modules.Register("notifications", prep, start, nil, "database", "base") + module = modules.Register("notifications", prep, start, nil, "database", "config", "base") } func prep() error { @@ -22,6 +24,43 @@ func start() error { return err } + showConfigLoadingErrors() + go module.StartServiceWorker("cleaner", 1*time.Second, cleaner) return nil } + +func showConfigLoadingErrors() { + validationErrors := config.GetLoadedConfigValidationErrors() + if len(validationErrors) == 0 { + return + } + + // Trigger a module error for more awareness. + module.Error( + "config:validation-errors-on-load", + "Invalid Settings", + "Some current settings are invalid. Please update them and restart the Portmaster.", + ) + + // Send one notification per invalid setting. + for _, validationError := range config.GetLoadedConfigValidationErrors() { + NotifyError( + fmt.Sprintf("config:validation-error:%s", validationError.Option.Key), + fmt.Sprintf("Invalid Setting for %s", validationError.Option.Name), + fmt.Sprintf(`Your current setting for %s is invalid: %s + +Please update the setting and restart the Portmaster, until then the default value is used.`, + validationError.Option.Name, + validationError.Err.Error(), + ), + Action{ + Text: "Change", + Type: ActionTypeOpenSetting, + Payload: &ActionTypeOpenSettingPayload{ + Key: validationError.Option.Key, + }, + }, + ) + } +}