From 2252135fadb8c579ad27345e3203be755130e3cd Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Mon, 20 Jun 2022 15:20:18 +0200 Subject: 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 --- internal/config/config.go | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'internal/config') 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) -} -- cgit v1.2.3