From 75c0eb3a016003315a8239f908fe798b3627e769 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Thu, 8 Oct 2026 16:44:42 +0530 Subject: [PATCH 1/2] fix: return error when config file cannot be parsed Signed-off-by: Sarthak --- pkg/config/config_test.go | 19 +++++++++++++++++++ pkg/config/localconfig.go | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 84860d8..088ed42 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -459,3 +459,22 @@ func TestWatchConfig(t *testing.T) { } } } + +func TestReadMalformedConfig(t *testing.T) { + tmpDir := t.TempDir() + configPath := filepath.Join(tmpDir, "config") + watchPath := filepath.Join(tmpDir, "watch") + + // A tab used for indentation is invalid YAML. + malformed := []byte("contexts:\n\t- name: ctx1\n") + require.NoError(t, os.WriteFile(configPath, malformed, 0o600)) + require.NoError(t, os.WriteFile(watchPath, malformed, 0o600)) + + cfg, err := ReadLocalConfig(configPath) + assert.Error(t, err, "Should fail on malformed YAML") + assert.Nil(t, cfg) + + wCfg, err := ReadLocalWatchConfig(watchPath) + assert.Error(t, err, "Should fail on malformed YAML") + assert.Nil(t, wCfg) +} diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 2aaa1b1..277b28a 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -104,6 +104,9 @@ func ReadLocalConfig(path string) (*LocalConfig, error) { if os.IsNotExist(err) { return nil, nil } + if err != nil { + return nil, fmt.Errorf("reading config %s: %w", path, err) + } err = ValidateLocalConfig(config) if err != nil { @@ -417,6 +420,9 @@ func ReadLocalWatchConfig(path string) (*WatchConfig, error) { if os.IsNotExist(err) { return nil, nil } + if err != nil { + return nil, fmt.Errorf("reading config %s: %w", path, err) + } return &config, nil } From 5f239b9c8cc8020d72b11f3059ebf8dbe4db4eb8 Mon Sep 17 00:00:00 2001 From: Sarthak Date: Sat, 10 Oct 2026 22:03:09 +0530 Subject: [PATCH 2/2] fix: use the centralized error-handling mechanism in ReadLocalConfig ReadLocalConfig and ReadLocalWatchConfig returned a plain fmt.Errorf for a malformed config file instead of going through errors.Wrap like the rest of pkg/*. Per documentation/error-handling.md, a malformed local input file is errors.KindUsage, so wrap it that way and assert the kind in TestReadMalformedConfig. Signed-off-by: Sarthak --- pkg/config/config_test.go | 3 +++ pkg/config/localconfig.go | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 088ed42..590ff37 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -28,6 +28,7 @@ import ( "testing" "time" + "github.com/microcks/microcks-cli/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -473,8 +474,10 @@ func TestReadMalformedConfig(t *testing.T) { cfg, err := ReadLocalConfig(configPath) assert.Error(t, err, "Should fail on malformed YAML") assert.Nil(t, cfg) + assert.Equal(t, errors.KindUsage, errors.KindOf(err), "a malformed local config is the user's input, not an environment or connection failure") wCfg, err := ReadLocalWatchConfig(watchPath) assert.Error(t, err, "Should fail on malformed YAML") assert.Nil(t, wCfg) + assert.Equal(t, errors.KindUsage, errors.KindOf(err), "a malformed local watch config is the user's input, not an environment or connection failure") } diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 277b28a..9358cf1 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -22,6 +22,7 @@ import ( "path/filepath" "slices" + "github.com/microcks/microcks-cli/pkg/errors" configUtil "github.com/microcks/microcks-cli/pkg/util" ) @@ -105,7 +106,7 @@ func ReadLocalConfig(path string) (*LocalConfig, error) { return nil, nil } if err != nil { - return nil, fmt.Errorf("reading config %s: %w", path, err) + return nil, errors.Wrapf(errors.KindUsage, "reading config %s: %w", path, err) } err = ValidateLocalConfig(config) @@ -421,7 +422,7 @@ func ReadLocalWatchConfig(path string) (*WatchConfig, error) { return nil, nil } if err != nil { - return nil, fmt.Errorf("reading config %s: %w", path, err) + return nil, errors.Wrapf(errors.KindUsage, "reading config %s: %w", path, err) } return &config, nil