Skip to content
Snippets Groups Projects
Verified Commit 8b0d5f5b authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: error handling #5

parent 572da8c9
No related branches found
No related tags found
No related merge requests found
......@@ -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.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()
}
}()
if mnemonic == "" {
......
......@@ -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()
}
}()
if err := validateConfig[C](config); err != nil {
......
......@@ -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()
}
}()
copyOf := func(s C) C {
......
......@@ -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()
}
}()
if prefix != "" {
......
......@@ -4,6 +4,7 @@
package configuration
type ErrorEvent struct {
Errors []error
}
type ErrorHook interface {
......
......@@ -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()
}
}()
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()
}
}()
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()
}
}()
switch format {
......
......@@ -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()
}
}()
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()
}
}()
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()
}
}()
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()
}
}()
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()
}
}()
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()
}
}()
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()
}
}()
if name == "" {
......
......@@ -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()
}
}()
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()
}
}()
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() {
......
......@@ -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()
}
}()
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()
}
}()
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()
}
}()
if err := mergo.Merge(&s.config, defaults); err != nil {
......
......@@ -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()
}
}()
err := runOnTags(&s.config, []string{"default"}, func(v string, field reflect.Value) {
......
......@@ -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()
}
}()
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()
}
}()
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()
}
}()
if s.fileWatch.watcher == nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment