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:
Abhisek Datta
2026-05-21 19:34:29 +05:30
committed by GitHub
co-authored by Claude
parent 0e3bb52f8c
commit b59c3358e8
13 changed files with 222 additions and 36 deletions
+7
View File
@@ -8,6 +8,7 @@ import (
"strings"
"testing"
"github.com/safedep/pmg/usefulerror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -96,6 +97,9 @@ func TestProfileDiffUnknownProfile(t *testing.T) {
assert.Empty(t, stdout)
assert.Empty(t, stderr)
assert.Contains(t, err.Error(), "no-such-profile-xyz")
usefulErr, ok := usefulerror.AsUsefulError(err)
require.True(t, ok, "diff error should expose a UsefulError through Unwrap")
assert.Equal(t, usefulerror.ErrCodeNotFound, usefulErr.Code())
}
func TestProfileDiffUnknownDriver(t *testing.T) {
@@ -107,6 +111,9 @@ func TestProfileDiffUnknownDriver(t *testing.T) {
assert.Empty(t, stdout)
assert.Empty(t, stderr)
assert.Contains(t, err.Error(), "unknown driver")
usefulErr, ok := usefulerror.AsUsefulError(err)
require.True(t, ok, "diff error should expose a UsefulError through Unwrap")
assert.Equal(t, usefulerror.ErrCodeInvalidArgument, usefulErr.Code())
}
func TestProfileDiffMissingProfileShowsUsage(t *testing.T) {