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:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@@ -62,6 +63,56 @@ func notFoundError(message, help string) error {
|
||||
Wrap(errors.New(message))
|
||||
}
|
||||
|
||||
// Idempotent: returns err unchanged when nil or already useful, so call
|
||||
// sites can apply it without losing more precise pre-classified errors.
|
||||
func wrapUseful(err error, code, help string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if _, ok := usefulerror.AsUsefulError(err); ok {
|
||||
return err
|
||||
}
|
||||
return usefulerror.Useful().
|
||||
WithCode(code).
|
||||
WithHumanError(err.Error()).
|
||||
WithHelp(help).
|
||||
Wrap(err)
|
||||
}
|
||||
|
||||
func profileLoadError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if _, ok := usefulerror.AsUsefulError(err); ok {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case errors.Is(err, pmgsandbox.ErrProfileNotFound):
|
||||
return wrapUseful(err, usefulerror.ErrCodeNotFound,
|
||||
"Use `pmg sandbox profile list` to see available profiles, or pass an existing profile YAML path.")
|
||||
case errors.Is(err, pmgsandbox.ErrProfileInvalid):
|
||||
return wrapUseful(err, usefulerror.ErrCodeInvalidArgument,
|
||||
"Check the profile YAML for syntax/schema issues and verify any 'inherits:' parent name.")
|
||||
}
|
||||
return wrapUseful(err, usefulerror.ErrCodeUnknown,
|
||||
"Failed to load the sandbox profile. Run with --verbose for the underlying cause.")
|
||||
}
|
||||
|
||||
func registryInitError(err error) error {
|
||||
return wrapUseful(err, ioErrorCode(err, usefulerror.ErrCodeUnknown),
|
||||
"Failed to initialise the sandbox profile registry. Run with --verbose for details.")
|
||||
}
|
||||
|
||||
func ioErrorCode(err error, fallback string) string {
|
||||
switch {
|
||||
case errors.Is(err, fs.ErrPermission):
|
||||
return usefulerror.ErrCodePermissionDenied
|
||||
case errors.Is(err, fs.ErrNotExist):
|
||||
return usefulerror.ErrCodeNotFound
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func writeJSONIndent(out io.Writer, v any) error {
|
||||
enc := json.NewEncoder(out)
|
||||
enc.SetIndent("", " ")
|
||||
|
||||
Reference in New Issue
Block a user