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
+11 -4
View File
@@ -8,6 +8,7 @@ import (
"regexp"
"strings"
"github.com/safedep/pmg/usefulerror"
"github.com/spf13/cobra"
)
@@ -59,7 +60,7 @@ func runProfileInit(out io.Writer, name string, opts *profileInitOptions, factor
registry, err := factory()
if err != nil {
return err
return registryInitError(err)
}
userDir := registry.UserProfileDir()
@@ -93,17 +94,23 @@ func runProfileInit(out io.Writer, name string, opts *profileInitOptions, factor
"Choose a different profile name, or edit the existing profile file to merge changes.",
)
} else if !os.IsNotExist(err) {
return fmt.Errorf("failed to stat %s: %w", target, err)
return wrapUseful(fmt.Errorf("failed to stat %s: %w", target, err),
ioErrorCode(err, usefulerror.ErrCodeUnknown),
"Could not stat the target profile path. Check the user profile directory permissions.")
}
if err := os.MkdirAll(userDir, 0o755); err != nil {
return fmt.Errorf("failed to create user profile directory %s: %w", userDir, err)
return wrapUseful(fmt.Errorf("failed to create user profile directory %s: %w", userDir, err),
ioErrorCode(err, usefulerror.ErrCodePermissionDenied),
"Could not create the user profile directory. Check filesystem permissions for "+userDir+".")
}
content := renderScaffold(name, opts.description, opts.from, pms, placeholderPM)
if err := os.WriteFile(target, []byte(content), 0o644); err != nil {
return fmt.Errorf("failed to write %s: %w", target, err)
return wrapUseful(fmt.Errorf("failed to write %s: %w", target, err),
ioErrorCode(err, usefulerror.ErrCodePermissionDenied),
"Could not write the scaffolded profile. Check filesystem permissions for "+target+".")
}
_, err = fmt.Fprintln(out, target)