refactor : Refactor error handling to use dry/usefulerror (#283)

* update the go.sum

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* migrate most of the files to dry errors

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* update the rest of the files

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* fixs the review comments

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

* chores

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>

---------

Signed-off-by: DivyanshuVortex <divyanshuchandra9027@gmail.com>
Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
This commit is contained in:
Divyanshu
2026-05-24 12:22:19 +05:30
committed by GitHub
co-authored by Abhisek Datta
parent e5fe0df82e
commit 20e01d5cae
33 changed files with 234 additions and 639 deletions
+13 -12
View File
@@ -11,7 +11,8 @@ import (
"github.com/safedep/pmg/internal/ui"
pmgsandbox "github.com/safedep/pmg/sandbox"
"github.com/safedep/pmg/usefulerror"
"github.com/safedep/dry/usefulerror"
"github.com/safedep/pmg/errcodes"
"github.com/spf13/cobra"
)
@@ -48,16 +49,16 @@ func validateDriver(name string) error {
}
func invalidArgumentError(message, help string) error {
return usefulerror.Useful().
WithCode(usefulerror.ErrCodeInvalidArgument).
return usefulerror.NewUsefulError().
WithCode(errcodes.InvalidArgument).
WithHumanError(message).
WithHelp(help).
Wrap(errors.New(message))
}
func notFoundError(message, help string) error {
return usefulerror.Useful().
WithCode(usefulerror.ErrCodeNotFound).
return usefulerror.NewUsefulError().
WithCode(errcodes.NotFound).
WithHumanError(message).
WithHelp(help).
Wrap(errors.New(message))
@@ -72,7 +73,7 @@ func wrapUseful(err error, code, help string) error {
if _, ok := usefulerror.AsUsefulError(err); ok {
return err
}
return usefulerror.Useful().
return usefulerror.NewUsefulError().
WithCode(code).
WithHumanError(err.Error()).
WithHelp(help).
@@ -88,27 +89,27 @@ func profileLoadError(err error) error {
}
switch {
case errors.Is(err, pmgsandbox.ErrProfileNotFound):
return wrapUseful(err, usefulerror.ErrCodeNotFound,
return wrapUseful(err, errcodes.NotFound,
"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,
return wrapUseful(err, errcodes.InvalidArgument,
"Check the profile YAML for syntax/schema issues and verify any 'inherits:' parent name.")
}
return wrapUseful(err, usefulerror.ErrCodeUnknown,
return wrapUseful(err, errcodes.Unknown,
"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),
return wrapUseful(err, ioErrorCode(err, errcodes.Unknown),
"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
return errcodes.PermissionDenied
case errors.Is(err, fs.ErrNotExist):
return usefulerror.ErrCodeNotFound
return errcodes.NotFound
}
return fallback
}