mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix(sandbox): classify helper-tool errors with usefulerror (#272)
* fix(sandbox): classify helper-tool errors with usefulerror Sandbox helper commands (profile lint/diff/show/init/list) used to bubble up plain fmt.Errorf chains from the registry layer, which the TUI then classified as Unknown and decorated with a bug-report link. Wrap each error path at the cmd/sandbox boundary so the TUI prints NotFound, InvalidArgument, or PermissionDenied with actionable hints instead. Closes #269 * refactor(sandbox): classify registry errors via sentinel wrapping Replace the fragile substring match in profileLoadError with errors.Is against new sandbox.ErrProfileNotFound / sandbox.ErrProfileInvalid sentinels. Every fmt.Errorf in registry.go that previously communicated "missing" or "malformed" by message text now wraps the corresponding sentinel, so the cmd layer can classify without inspecting strings. * fix(sandbox): detect IO error class when wrapping helper errors Replace static ErrCodeUnknown / ErrCodePermissionDenied wrappings with ioErrorCode, which inspects the error chain for fs.ErrPermission and fs.ErrNotExist before falling back. Applied to runProfileList (where an unreadable user profile directory now classifies as PermissionDenied), registryInitError, and the stat/MkdirAll/WriteFile paths in profile init. Also drop redundant doc comments on helpers whose names are self-evident. --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -24,6 +25,33 @@ func TestNewDefaultProfileRegistry(t *testing.T) {
|
||||
assert.NotNil(t, pypiRestrictive)
|
||||
}
|
||||
|
||||
func TestGetProfileUnknownWrapsErrProfileNotFound(t *testing.T) {
|
||||
registry, err := newDefaultProfileRegistry()
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = registry.GetProfile("definitely-not-a-real-profile-name")
|
||||
assert.Error(t, err)
|
||||
assert.True(t, errors.Is(err, ErrProfileNotFound),
|
||||
"unknown profile should wrap ErrProfileNotFound, got %T: %v", err, err)
|
||||
}
|
||||
|
||||
func TestLoadCustomProfileInvalidYAMLWrapsErrProfileInvalid(t *testing.T) {
|
||||
registry, err := newDefaultProfileRegistry()
|
||||
assert.NoError(t, err)
|
||||
|
||||
tempFile, err := os.CreateTemp(t.TempDir(), "bad-policy-*.yml")
|
||||
assert.NoError(t, err)
|
||||
defer func() { _ = tempFile.Close() }()
|
||||
|
||||
_, err = tempFile.WriteString("name: bad\npackage_managers:\n - npm\n invalid: : :\n")
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = registry.LoadCustomProfile(tempFile.Name())
|
||||
assert.Error(t, err)
|
||||
assert.True(t, errors.Is(err, ErrProfileInvalid),
|
||||
"YAML parse failure should wrap ErrProfileInvalid, got %T: %v", err, err)
|
||||
}
|
||||
|
||||
func TestLoadCustomProfile(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
@@ -188,6 +216,8 @@ func TestLoadCustomProfileWithInheritance(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
assert.Nil(t, policy)
|
||||
assert.Contains(t, err.Error(), "inherits from unknown profile")
|
||||
assert.True(t, errors.Is(err, ErrProfileNotFound),
|
||||
"missing parent profile should wrap ErrProfileNotFound")
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user