From 8b0d5f5bb22ee226550855b018d949a433bc07d8 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Tue, 15 Aug 2023 13:44:34 +0200 Subject: [PATCH] feat: error handling #5 --- api.go | 10 ++-------- change.go | 5 +---- copyable.go | 5 +---- env.go | 5 +---- error-handler.go | 1 + export.go | 15 +++------------ file.go | 35 +++++++---------------------------- flags.go | 22 ++++++++-------------- import.go | 15 +++------------ settings.go | 5 +---- watch.go | 15 +++------------ 11 files changed, 31 insertions(+), 102 deletions(-) diff --git a/api.go b/api.go index b8638a5..c89bfa3 100644 --- a/api.go +++ b/api.go @@ -13,11 +13,8 @@ func New[C any](defaults C) *Settings[C] { s := &Settings[C]{} - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() s.initDefaults() @@ -43,11 +40,8 @@ func New[C any](defaults C) *Settings[C] { // The mnemonic is used to identify the configuration in the configuration file func (s *Settings[C]) SetMnemonic(mnemonic string) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if mnemonic == "" { diff --git a/change.go b/change.go index df2b1bd..221c455 100644 --- a/change.go +++ b/change.go @@ -23,11 +23,8 @@ func (s *Settings[C]) setConfigInternal(config C) (*Settings[C], diff.Changelog) err error ) - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if err := validateConfig[C](config); err != nil { diff --git a/copyable.go b/copyable.go index 7a64e4b..4e30abb 100644 --- a/copyable.go +++ b/copyable.go @@ -16,11 +16,8 @@ func (s *Settings[C]) Copy(m map[string]any) { s.Lock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() copyOf := func(s C) C { diff --git a/env.go b/env.go index 050a920..a809a23 100644 --- a/env.go +++ b/env.go @@ -15,11 +15,8 @@ func (s *Settings[C]) InitFromEnv(prefix string) *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if prefix != "" { diff --git a/error-handler.go b/error-handler.go index fee0f7c..92af915 100644 --- a/error-handler.go +++ b/error-handler.go @@ -4,6 +4,7 @@ package configuration type ErrorEvent struct { + Errors []error } type ErrorHook interface { diff --git a/export.go b/export.go index e1d9aba..9f2424d 100644 --- a/export.go +++ b/export.go @@ -34,11 +34,8 @@ func (s *Settings[C]) writeProperties(writer io.Writer) error { m, errors := getMapForProperties[C](s.config) - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if len(errors) > 0 { @@ -61,11 +58,8 @@ func (s *Settings[C]) WriteFile(fn string, format Format) *Settings[C] { var file *os.File - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if fn == "" { @@ -89,11 +83,8 @@ func (s *Settings[C]) Write(writer io.Writer, format Format) *Settings[C] { var err error - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() switch format { diff --git a/file.go b/file.go index b934f0e..62e1c5b 100644 --- a/file.go +++ b/file.go @@ -39,11 +39,8 @@ func (s *Settings[C]) AddFile(file string, format ...Format) *Settings[C] { var f Format - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if format == nil || len(format) == 0 { @@ -112,11 +109,8 @@ func (s *Settings[C]) AddDirectory(d string) *Settings[C] { func (s *Settings[C]) sanitizeDirectories() { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() wd, err := os.Getwd() @@ -163,11 +157,8 @@ func (s *Settings[C]) AddWorkingDirectory() *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() current, err := os.Getwd() @@ -186,11 +177,8 @@ func (s *Settings[C]) AddEtcDirectory() *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.mnemonic == "" { @@ -218,11 +206,8 @@ func (s *Settings[C]) AddUserConfigDirectory() *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.mnemonic == "" { @@ -253,11 +238,8 @@ func (s *Settings[C]) SetDefaultDirectories() *Settings[C] { func (s *Settings[C]) SetFileFormat(format Format) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if slices.Contains(availableFormats, format) { @@ -272,11 +254,8 @@ func (s *Settings[C]) SetFileFormat(format Format) *Settings[C] { // SetFileName sets the name of the configuration file func (s *Settings[C]) SetFileName(name string) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if name == "" { diff --git a/flags.go b/flags.go index 0fb13a8..f53b989 100644 --- a/flags.go +++ b/flags.go @@ -13,20 +13,17 @@ import ( // The file is read from the flag specified by the name func (s *Settings[C]) AddFileFromFlagSet(flagset *flag.FlagSet, name string, format Format) *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() - flag := flagset.Lookup(name) - if flag == nil { + flg := flagset.Lookup(name) + if flg == nil { s.errors = append(s.errors, newFlagDoesNotExistError(name)) return s } - path := flag.Value.String() + path := flg.Value.String() if path == "" { s.errors = append(s.errors, FileNameEmptyError) return s @@ -40,22 +37,19 @@ func (s *Settings[C]) InitFromFlagSet(flagset *flag.FlagSet) *Settings[C] { s.Lock() defer s.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() err := runOnTags(&s.config, []string{flagTagKey}, func(k string, field reflect.Value) { - flag := flagset.Lookup(k) - if flag == nil { + flg := flagset.Lookup(k) + if flg == nil { s.errors = append(s.errors, newFlagNotFoundError(k)) return // flag not found } - v := flag.Value + v := flg.Value t := reflect.TypeOf(v) if !field.CanSet() { diff --git a/import.go b/import.go index e277c60..28bc465 100644 --- a/import.go +++ b/import.go @@ -51,11 +51,8 @@ func (s *Settings[C]) importStream(r reader, f ...func(n *C)) { var c C var err error - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() err = nil // reset error @@ -141,11 +138,8 @@ func (s *Settings[C]) importFiles() { s.fileWatch.Unlock() }() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() for _, d := range s.files.directories { @@ -198,11 +192,8 @@ func (s *Settings[C]) Import() *Settings[C] { s.importFiles() s.importStreams() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if err := mergo.Merge(&s.config, defaults); err != nil { diff --git a/settings.go b/settings.go index a76b270..2014632 100644 --- a/settings.go +++ b/settings.go @@ -39,11 +39,8 @@ type Settings[C any] struct { func (s *Settings[C]) initDefaults() *Settings[C] { - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() err := runOnTags(&s.config, []string{"default"}, func(v string, field reflect.Value) { diff --git a/watch.go b/watch.go index 4d19b19..5b90b6e 100644 --- a/watch.go +++ b/watch.go @@ -13,11 +13,8 @@ func (s *Settings[C]) initWatch() *Settings[C] { var err error - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.fileWatch.watcher != nil { @@ -40,11 +37,8 @@ func (s *Settings[C]) StopWatching() *Settings[C] { s.fileWatch.Lock() defer s.fileWatch.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.fileWatch.watcher == nil { @@ -89,11 +83,8 @@ func (s *Settings[C]) Watch() *Settings[C] { s.fileWatch.Lock() defer s.fileWatch.Unlock() - errorCount := len(s.errors) defer func() { - if len(s.errors) > errorCount { - s.notifyErrorHooks() - } + s.notifyErrorHooks() }() if s.fileWatch.watcher == nil { -- GitLab