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
+8 -7
View File
@@ -12,7 +12,8 @@ import (
"github.com/safedep/pmg/internal/audit"
"github.com/safedep/pmg/sandbox"
"github.com/safedep/pmg/sandbox/platform"
"github.com/safedep/pmg/usefulerror"
"github.com/safedep/dry/usefulerror"
"github.com/safedep/pmg/errcodes"
)
type applySandboxConfig struct {
@@ -58,8 +59,8 @@ func ApplySandbox(ctx context.Context, cmd *exec.Cmd, pmName string, opts ...app
policy, err = registry.GetProfile(cfg.SandboxProfileOverride)
if err != nil {
return nil, usefulerror.Useful().
WithCode(usefulerror.ErrCodeInvalidArgument).
return nil, usefulerror.NewUsefulError().
WithCode(errcodes.InvalidArgument).
WithHumanError(fmt.Sprintf("Failed to load sandbox profile override: %s", cfg.SandboxProfileOverride)).
WithHelp("Please verify the sandbox profile path and try again.").
WithAdditionalHelp("See more at: https://github.com/safedep/pmg/blob/main/docs/sandbox.md").
@@ -74,8 +75,8 @@ func ApplySandbox(ctx context.Context, cmd *exec.Cmd, pmName string, opts ...app
// disable for the package manager in the config.
policyRef, exists := cfg.Config.Sandbox.Policies[pmName]
if !exists {
return nil, usefulerror.Useful().
WithCode(usefulerror.ErrCodeNotFound).
return nil, usefulerror.NewUsefulError().
WithCode(errcodes.NotFound).
WithHumanError(fmt.Sprintf("No sandbox policy configured for %s", pmName)).
WithHelp("Please configure a sandbox policy for this package manager in the config file.").
WithAdditionalHelp("See https://github.com/safedep/pmg/blob/main/docs/sandbox.md for more information.").
@@ -137,8 +138,8 @@ func ApplySandbox(ctx context.Context, cmd *exec.Cmd, pmName string, opts ...app
}
if !sb.IsAvailable() {
return nil, usefulerror.Useful().
WithCode(usefulerror.ErrCodeInvalidArgument).
return nil, usefulerror.NewUsefulError().
WithCode(errcodes.InvalidArgument).
WithHumanError(fmt.Sprintf("Sandbox %s is required but not available", sb.Name())).
WithHelp("Please install the sandbox provider and try again.").
WithAdditionalHelp("See more at: https://github.com/safedep/pmg/blob/main/docs/sandbox.md").
+4 -3
View File
@@ -6,7 +6,8 @@ import (
"github.com/safedep/dry/log"
"github.com/safedep/pmg/config"
"github.com/safedep/pmg/sandbox"
"github.com/safedep/pmg/usefulerror"
"github.com/safedep/dry/usefulerror"
"github.com/safedep/pmg/errcodes"
)
// WrapCommandExecutionError converts a package manager execution error into a
@@ -28,8 +29,8 @@ func WrapCommandExecutionError(err error, result *sandbox.ExecutionResult, exitC
}
help := "Check the package manager command and its arguments"
builder := usefulerror.Useful().
WithCode(usefulerror.ErrCodePackageManagerExecutionFailed).
builder := usefulerror.NewUsefulError().
WithCode(errcodes.PackageManagerExecutionFailed).
WithHumanError(humanError).
WithHelp(help)
+4 -3
View File
@@ -7,7 +7,8 @@ import (
"testing"
"github.com/safedep/pmg/sandbox"
"github.com/safedep/pmg/usefulerror"
"github.com/safedep/dry/usefulerror"
"github.com/safedep/pmg/errcodes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -61,7 +62,7 @@ func TestWrapCommandExecutionErrorDoesNotAttributeFailureToSandbox(t *testing.T)
usefulErr, ok := usefulerror.AsUsefulError(err)
require.True(t, ok)
assert.Equal(t, usefulerror.ErrCodePackageManagerExecutionFailed, usefulErr.Code())
assert.Equal(t, errcodes.PackageManagerExecutionFailed, usefulErr.Code())
assert.Equal(t, "Package manager command exited with code: 1", usefulErr.HumanError())
assert.NotContains(t, usefulErr.Help(), "./.env")
assert.Contains(t, usefulErr.AdditionalHelp(), "pmg sandbox violations list")
@@ -76,7 +77,7 @@ func TestWrapCommandExecutionErrorOmitsBreadcrumbWhenNoViolations(t *testing.T)
usefulErr, ok := usefulerror.AsUsefulError(err)
require.True(t, ok)
assert.Equal(t, usefulerror.ErrCodePackageManagerExecutionFailed, usefulErr.Code())
assert.Equal(t, errcodes.PackageManagerExecutionFailed, usefulErr.Code())
assert.NotContains(t, usefulErr.AdditionalHelp(), "pmg sandbox violations list")
}