From 27448f33dce9141316bb4ea6d905305d8a846cac Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 15 Apr 2024 22:52:41 +0200 Subject: wip: verbose error handling --- internal/config/config.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'internal/config') diff --git a/internal/config/config.go b/internal/config/config.go index aa46e6a..12b9395 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,7 +1,10 @@ package config import ( + "io/fs" + "github.com/BurntSushi/toml" + "github.com/pkg/errors" ) type Taxonomy struct { @@ -30,8 +33,19 @@ type Config struct { Menus map[string][]MenuItem } -func GetConfig() (error, Config) { +func GetConfig() (*Config, error) { config := Config{} _, err := toml.DecodeFile("config.toml", &config) - return err, config + if err != nil { + var pathError *fs.PathError + var tomlError toml.ParseError + if errors.As(err, &pathError) { + return nil, errors.WithMessage(err, "could not read configuration") + } else if errors.As(err, &tomlError) { + return nil, errors.WithMessage(err, tomlError.ErrorWithUsage()) + } else { + return nil, errors.Wrap(err, "config error") + } + } + return &config, nil } -- cgit 1.4.1