Skip to content

Commit 5bdfb4f

Browse files
authored
feat(config): add a typed error on config file not found (#264)
1 parent 7972939 commit 5bdfb4f

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

scw/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"syscall"
89
"text/template"
910

1011
"github.com/scaleway/scaleway-sdk-go/internal/auth"
@@ -170,6 +171,9 @@ func LoadConfigFromPath(path string) (*Config, error) {
170171

171172
file, err := ioutil.ReadFile(path)
172173
if err != nil {
174+
if pathError, isPathError := err.(*os.PathError); isPathError && pathError.Err == syscall.ENOENT {
175+
return nil, configFileNotFound(pathError.Path)
176+
}
173177
return nil, errors.Wrap(err, "cannot read config file")
174178
}
175179

scw/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func TestLoadProfileAndActiveProfile(t *testing.T) {
265265
},
266266
{
267267
name: "No config",
268-
expectedError: "scaleway-sdk-go: cannot read config file: open {HOME}/.config/scw/config.yaml: no such file or directory",
268+
expectedError: "scaleway-sdk-go: cannot read config file {HOME}/.config/scw/config.yaml: no such file or directory",
269269
env: map[string]string{
270270
"HOME": "{HOME}",
271271
},

scw/errors.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,18 @@ func (e InvalidClientOptionError) IsScwSdkError() {}
270270
func (e InvalidClientOptionError) Error() string {
271271
return fmt.Sprintf("scaleway-sdk-go: %s", e.errorType)
272272
}
273+
274+
// ConfigFileNotFound indicates that the config file could not be found
275+
type ConfigFileNotFoundError struct {
276+
path string
277+
}
278+
279+
func configFileNotFound(path string) *ConfigFileNotFoundError {
280+
return &ConfigFileNotFoundError{path: path}
281+
}
282+
283+
// ConfigFileNotFoundError implements the SdkError interface
284+
func (e ConfigFileNotFoundError) IsScwSdkError() {}
285+
func (e ConfigFileNotFoundError) Error() string {
286+
return fmt.Sprintf("scaleway-sdk-go: cannot read config file %s: no such file or directory", e.path)
287+
}

scw/load_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestLoad(t *testing.T) {
102102
env: map[string]string{
103103
scwConfigPathEnv: "{HOME}/fake/test.conf",
104104
},
105-
expectedError: "scaleway-sdk-go: cannot read config file: open {HOME}/fake/test.conf: no such file or directory",
105+
expectedError: "scaleway-sdk-go: cannot read config file {HOME}/fake/test.conf: no such file or directory",
106106
},
107107
{
108108
name: "Err: custom-path config with invalid V2",

0 commit comments

Comments
 (0)