Skip to content

Commit f6de111

Browse files
authored
clientconfig: fix file load error checks (#147)
In addition wraps underline Errorf's errors. Requires Go 1.13+ Fix #145. Signed-off-by: Vladimir Ermakov <[email protected]>
1 parent 13abf22 commit f6de111

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

openstack/clientconfig/requests.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package clientconfig
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
7+
"os"
68
"reflect"
79
"strings"
810

@@ -122,7 +124,7 @@ func LoadCloudsYAML() (map[string]Cloud, error) {
122124
var clouds Clouds
123125
err = yaml.Unmarshal(content, &clouds)
124126
if err != nil {
125-
return nil, fmt.Errorf("failed to unmarshal yaml: %v", err)
127+
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
126128
}
127129

128130
return clouds.Clouds, nil
@@ -138,7 +140,7 @@ func LoadSecureCloudsYAML() (map[string]Cloud, error) {
138140

139141
_, content, err := FindAndReadSecureCloudsYAML()
140142
if err != nil {
141-
if err.Error() == "no secure.yaml file found" {
143+
if errors.Is(err, os.ErrNotExist) {
142144
// secure.yaml is optional so just ignore read error
143145
return secureClouds.Clouds, nil
144146
}
@@ -147,7 +149,7 @@ func LoadSecureCloudsYAML() (map[string]Cloud, error) {
147149

148150
err = yaml.Unmarshal(content, &secureClouds)
149151
if err != nil {
150-
return nil, fmt.Errorf("failed to unmarshal yaml: %v", err)
152+
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
151153
}
152154

153155
return secureClouds.Clouds, nil
@@ -163,7 +165,7 @@ func LoadPublicCloudsYAML() (map[string]Cloud, error) {
163165

164166
_, content, err := FindAndReadPublicCloudsYAML()
165167
if err != nil {
166-
if err.Error() == "no clouds-public.yaml file found" {
168+
if errors.Is(err, os.ErrNotExist) {
167169
// clouds-public.yaml is optional so just ignore read error
168170
return publicClouds.Clouds, nil
169171
}
@@ -173,7 +175,7 @@ func LoadPublicCloudsYAML() (map[string]Cloud, error) {
173175

174176
err = yaml.Unmarshal(content, &publicClouds)
175177
if err != nil {
176-
return nil, fmt.Errorf("failed to unmarshal yaml: %v", err)
178+
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
177179
}
178180

179181
return publicClouds.Clouds, nil
@@ -189,7 +191,7 @@ func GetCloudFromYAML(opts *ClientOpts) (*Cloud, error) {
189191

190192
clouds, err := yamlOpts.LoadCloudsYAML()
191193
if err != nil {
192-
return nil, fmt.Errorf("unable to load clouds.yaml: %s", err)
194+
return nil, fmt.Errorf("unable to load clouds.yaml: %w", err)
193195
}
194196

195197
// Determine which cloud to use.
@@ -236,7 +238,7 @@ func GetCloudFromYAML(opts *ClientOpts) (*Cloud, error) {
236238
if profileName != "" {
237239
publicClouds, err := yamlOpts.LoadPublicCloudsYAML()
238240
if err != nil {
239-
return nil, fmt.Errorf("unable to load clouds-public.yaml: %s", err)
241+
return nil, fmt.Errorf("unable to load clouds-public.yaml: %w", err)
240242
}
241243

242244
publicCloud, ok := publicClouds[profileName]
@@ -255,7 +257,7 @@ func GetCloudFromYAML(opts *ClientOpts) (*Cloud, error) {
255257
// can be found or merged.
256258
secureClouds, err := yamlOpts.LoadSecureCloudsYAML()
257259
if err != nil {
258-
return nil, fmt.Errorf("unable to load secure.yaml: %s", err)
260+
return nil, fmt.Errorf("unable to load secure.yaml: %w", err)
259261
}
260262

261263
if secureClouds != nil {

openstack/clientconfig/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func FindAndReadYAML(yamlFile string) (string, []byte, error) {
138138
// current directory
139139
cwd, err := os.Getwd()
140140
if err != nil {
141-
return "", nil, fmt.Errorf("unable to determine working directory: %s", err)
141+
return "", nil, fmt.Errorf("unable to determine working directory: %w", err)
142142
}
143143

144144
filename := filepath.Join(cwd, yamlFile)
@@ -166,7 +166,7 @@ func FindAndReadYAML(yamlFile string) (string, []byte, error) {
166166
return filename, content, err
167167
}
168168

169-
return "", nil, fmt.Errorf("no " + yamlFile + " file found")
169+
return "", nil, fmt.Errorf("no %s file found: %w", yamlFile, os.ErrNotExist)
170170
}
171171

172172
// fileExists checks for the existence of a file at a given location.

0 commit comments

Comments
 (0)