summaryrefslogtreecommitdiff
path: root/internal/config.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-02 14:34:35 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-02 14:34:35 +0200
commit466450f0c47bdc614e66326d90e5fc6fb56ae732 (patch)
treea01518a58d50d2f8449d37dadecc40e35c9f1fe1 /internal/config.go
parenta2a8efdcaad3d9b1852b1367a7cd7e8c5860cecf (diff)
Refactor: Wrap most errors in a custom type
Diffstat (limited to 'internal/config.go')
-rw-r--r--internal/config.go22
1 files changed, 19 insertions, 3 deletions
diff --git a/internal/config.go b/internal/config.go
index 47f773e..a135ac6 100644
--- a/internal/config.go
+++ b/internal/config.go
@@ -25,11 +25,11 @@ func (config *Config) GetFilename() string {
func (config *Config) Save(readStruct interface{}) error {
configDirErr := EnsureDirectory(config.Directory)
if configDirErr != nil {
- return configDirErr
+ return &ConfigSaveError{Err: configDirErr}
}
jsonString, marshalErr := json.Marshal(readStruct)
if marshalErr != nil {
- return marshalErr
+ return &ConfigSaveError{Err: marshalErr}
}
return ioutil.WriteFile(config.GetFilename(), jsonString, 0o644)
}
@@ -37,7 +37,23 @@ func (config *Config) Save(readStruct interface{}) error {
func (config *Config) Load(writeStruct interface{}) error {
bytes, readErr := ioutil.ReadFile(config.GetFilename())
if readErr != nil {
- return readErr
+ return &ConfigLoadError{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)
+}