Skip to content

Commit 96a5d65

Browse files
authored
Merge pull request #4744 from kersten/chore/wrap-errors-cli-external-plugins
🌱 (chore): improve error wrapping for external plugin discovery and config loading
2 parents 02168ca + 9165aa2 commit 96a5d65

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

pkg/cli/cli.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (c *CLI) getInfoFromConfigFile() error {
216216
// Read the project configuration file
217217
cfg := yamlstore.New(c.fs)
218218
if err := cfg.Load(); err != nil {
219-
return err
219+
return fmt.Errorf("error loading configuration: %w", err)
220220
}
221221

222222
return c.getInfoFromConfig(cfg.Config())
@@ -260,12 +260,12 @@ func (c *CLI) getInfoFromFlags(hasConfigFile bool) error {
260260

261261
// Parse the arguments
262262
if err := fs.Parse(os.Args[1:]); err != nil {
263-
return err
263+
return fmt.Errorf("could not parse flags: %w", err)
264264
}
265265

266266
// If any plugin key was provided, replace those from the project configuration file
267267
if pluginKeys, err := fs.GetStringSlice(pluginsFlag); err != nil {
268-
return err
268+
return fmt.Errorf("invalid flag %q: %w", pluginsFlag, err)
269269
} else if len(pluginKeys) != 0 {
270270
// Remove leading and trailing spaces and validate the plugin keys
271271
for i, key := range pluginKeys {
@@ -468,7 +468,11 @@ func (c CLI) metadata() plugin.CLIMetadata {
468468
//
469469
// If an error is found, command help and examples will be printed.
470470
func (c CLI) Run() error {
471-
return c.cmd.Execute()
471+
if err := c.cmd.Execute(); err != nil {
472+
return fmt.Errorf("error executing command: %w", err)
473+
}
474+
475+
return nil
472476
}
473477

474478
// Command returns the underlying root command.

pkg/cli/options.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func DiscoverExternalPlugins(filesystem afero.Fs) (ps []plugin.Plugin, err error
244244
pluginsRoot, err := retrievePluginsRoot(runtime.GOOS)
245245
if err != nil {
246246
logrus.Errorf("could not get plugins root: %v", err)
247-
return nil, err
247+
return nil, fmt.Errorf("could not get plugins root: %w", err)
248248
}
249249

250250
rootInfo, err := filesystem.Stat(pluginsRoot)
@@ -253,7 +253,7 @@ func DiscoverExternalPlugins(filesystem afero.Fs) (ps []plugin.Plugin, err error
253253
logrus.Debugf("External plugins dir %q does not exist, skipping external plugin parsing", pluginsRoot)
254254
return nil, nil
255255
}
256-
return nil, err
256+
return nil, fmt.Errorf("error getting stats for plugins %s: %w", pluginsRoot, err)
257257
}
258258
if !rootInfo.IsDir() {
259259
logrus.Debugf("External plugins path %q is not a directory, skipping external plugin parsing", pluginsRoot)
@@ -262,7 +262,7 @@ func DiscoverExternalPlugins(filesystem afero.Fs) (ps []plugin.Plugin, err error
262262

263263
pluginInfos, err := afero.ReadDir(filesystem, pluginsRoot)
264264
if err != nil {
265-
return nil, err
265+
return nil, fmt.Errorf("error reading plugins directory %q: %w", pluginsRoot, err)
266266
}
267267

268268
for _, pluginInfo := range pluginInfos {
@@ -273,7 +273,8 @@ func DiscoverExternalPlugins(filesystem afero.Fs) (ps []plugin.Plugin, err error
273273

274274
versions, err := afero.ReadDir(filesystem, filepath.Join(pluginsRoot, pluginInfo.Name()))
275275
if err != nil {
276-
return nil, err
276+
return nil, fmt.Errorf("error reading plugin directory %s: %w",
277+
filepath.Join(pluginsRoot, pluginInfo.Name()), err)
277278
}
278279

279280
for _, version := range versions {
@@ -284,7 +285,8 @@ func DiscoverExternalPlugins(filesystem afero.Fs) (ps []plugin.Plugin, err error
284285

285286
pluginFiles, err := afero.ReadDir(filesystem, filepath.Join(pluginsRoot, pluginInfo.Name(), version.Name()))
286287
if err != nil {
287-
return nil, err
288+
return nil, fmt.Errorf("error reading plugion version directory %q: %w",
289+
filepath.Join(pluginsRoot, pluginInfo.Name(), version.Name()), err)
288290
}
289291

290292
for _, pluginFile := range pluginFiles {
@@ -310,8 +312,8 @@ func DiscoverExternalPlugins(filesystem afero.Fs) (ps []plugin.Plugin, err error
310312
Args: parseExternalPluginArgs(),
311313
}
312314

313-
if err := ep.PVersion.Parse(version.Name()); err != nil {
314-
return nil, err
315+
if err = ep.PVersion.Parse(version.Name()); err != nil {
316+
return nil, fmt.Errorf("error parsing external plugin version %q: %w", version.Name(), err)
315317
}
316318

317319
logrus.Printf("Adding external plugin: %s", ep.Name())

pkg/cli/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ var _ = Describe("Discover external plugins", func() {
400400
_, err = DiscoverExternalPlugins(filesystem.FS)
401401
Expect(err).To(HaveOccurred())
402402

403-
Expect(err).To(Equal(errPluginsRoot))
403+
Expect(errors.Unwrap(err)).To(MatchError(errPluginsRoot))
404404
})
405405

406406
It("should skip parsing of directories if plugins root is not a directory", func() {

0 commit comments

Comments
 (0)