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:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/safedep/dry/log"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/platform"
|
||||
"github.com/safedep/pmg/usefulerror"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -53,7 +54,7 @@ func runProfileShow(out io.Writer, name string, opts *profileShowOptions, factor
|
||||
|
||||
registry, err := factory()
|
||||
if err != nil {
|
||||
return err
|
||||
return registryInitError(err)
|
||||
}
|
||||
|
||||
if opts.driver != "" {
|
||||
@@ -70,7 +71,7 @@ func runProfileShow(out io.Writer, name string, opts *profileShowOptions, factor
|
||||
func runProfileShowRaw(out io.Writer, name string, opts *profileShowOptions, registry pmgsandbox.ProfileRegistry) error {
|
||||
source, path, data, err := loadProfileSource(name, registry)
|
||||
if err != nil {
|
||||
return err
|
||||
return profileLoadError(err)
|
||||
}
|
||||
|
||||
if opts.jsonOut {
|
||||
@@ -93,7 +94,7 @@ func runProfileShowResolved(out io.Writer, name string, opts *profileShowOptions
|
||||
Home: opts.home,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return profileLoadError(err)
|
||||
}
|
||||
|
||||
if opts.jsonOut {
|
||||
@@ -106,7 +107,9 @@ func runProfileShowResolved(out io.Writer, name string, opts *profileShowOptions
|
||||
|
||||
data, err := yaml.Marshal(policy)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal resolved policy: %w", err)
|
||||
return wrapUseful(fmt.Errorf("failed to marshal resolved policy: %w", err),
|
||||
usefulerror.ErrCodeUnknown,
|
||||
"Failed to marshal the resolved policy to YAML. Re-run with --verbose for the underlying cause.")
|
||||
}
|
||||
|
||||
_, err = out.Write(data)
|
||||
@@ -119,12 +122,13 @@ func runProfileShowDriver(out io.Writer, name string, opts *profileShowOptions,
|
||||
Home: opts.home,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return profileLoadError(err)
|
||||
}
|
||||
|
||||
rendered, err := platform.Render(pmgsandbox.DriverName(opts.driver), policy)
|
||||
if err != nil {
|
||||
return err
|
||||
return wrapUseful(err, usefulerror.ErrCodeInvalidArgument,
|
||||
"Could not render the policy for the requested driver. Verify the driver is supported on this host.")
|
||||
}
|
||||
|
||||
if opts.jsonOut {
|
||||
|
||||
Reference in New Issue
Block a user