diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-06-20 15:20:18 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-06-20 15:20:18 +0200 |
| commit | 2252135fadb8c579ad27345e3203be755130e3cd (patch) | |
| tree | ed5a530e85b43736fc0bc28c927cfa8488f9199b /internal/config | |
| parent | 7af07c596166bf93b79a9d0816b1950dde360fb9 (diff) | |
Refactor: Errors to have one custom type that is to be wrapped
- For this an `internal/types` package is created with a custom error type
- This custom error type can give back the cause and traceback of an error
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 25 |
1 files changed, 6 insertions, 19 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index a9ebec7..a74bcd8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -5,6 +5,8 @@ import ( "fmt" "io/ioutil" "path" + + "github.com/jwijenbergh/eduvpn-common/internal/types" "github.com/jwijenbergh/eduvpn-common/internal/util" ) @@ -24,13 +26,14 @@ func (config *Config) GetFilename() string { } func (config *Config) Save(readStruct interface{}) error { + errorMessage := "failed saving configuration" configDirErr := util.EnsureDirectory(config.Directory) if configDirErr != nil { - return &ConfigSaveError{Err: configDirErr} + return &types.WrappedErrorMessage{Message: errorMessage, Err: configDirErr} } jsonString, marshalErr := json.Marshal(readStruct) if marshalErr != nil { - return &ConfigSaveError{Err: marshalErr} + return &types.WrappedErrorMessage{Message: errorMessage, Err: marshalErr} } return ioutil.WriteFile(config.GetFilename(), jsonString, 0o600) } @@ -38,23 +41,7 @@ func (config *Config) Save(readStruct interface{}) error { func (config *Config) Load(writeStruct interface{}) error { bytes, readErr := ioutil.ReadFile(config.GetFilename()) if readErr != nil { - return &ConfigLoadError{Err: readErr} + return &types.WrappedErrorMessage{Message: "failed loading configuration", Err: readErr} } return json.Unmarshal(bytes, writeStruct) } - -type ConfigSaveError struct { - Err error -} - -func (e *ConfigSaveError) Error() string { - return fmt.Sprintf("failed to save config with error: %v", e.Err) -} - -type ConfigLoadError struct { - Err error -} - -func (e *ConfigLoadError) Error() string { - return fmt.Sprintf("failed to load config with error: %v", e.Err) -} |
