mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
chore: Standardise Error Codes (#286)
* fix: Misc error handling fixes * fix: Sandbox error translation
This commit is contained in:
+1
-1
@@ -3,9 +3,9 @@ package cloud
|
||||
import (
|
||||
"github.com/safedep/dry/cloud"
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
+1
-1
@@ -3,9 +3,9 @@ package cloud
|
||||
import (
|
||||
"github.com/safedep/dry/cloud"
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
+2
-2
@@ -5,12 +5,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/analytics"
|
||||
"github.com/safedep/pmg/internal/audit"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/platform"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/platform"
|
||||
)
|
||||
|
||||
// stubProbe is a minimal probe used by tests.
|
||||
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/config"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
func sampleReport() *pmgsandbox.ViolationReport {
|
||||
|
||||
+12
-4
@@ -9,10 +9,10 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -70,7 +70,7 @@ func wrapUseful(err error, code, help string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if _, ok := usefulerror.AsUsefulError(err); ok {
|
||||
if hasUsefulError(err) {
|
||||
return err
|
||||
}
|
||||
return usefulerror.NewUsefulError().
|
||||
@@ -84,7 +84,7 @@ func profileLoadError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if _, ok := usefulerror.AsUsefulError(err); ok {
|
||||
if hasUsefulError(err) {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
@@ -99,6 +99,14 @@ func profileLoadError(err error) error {
|
||||
"Failed to load the sandbox profile. Run with --verbose for the underlying cause.")
|
||||
}
|
||||
|
||||
func hasUsefulError(err error) bool {
|
||||
// AsUsefulError also runs global converters for plain errors like
|
||||
// fs.ErrPermission. Contextual wrappers must only skip errors that already
|
||||
// carry UsefulError details, otherwise generic converters hide command help.
|
||||
var usefulErr usefulerror.UsefulError
|
||||
return errors.As(err, &usefulErr)
|
||||
}
|
||||
|
||||
func registryInitError(err error) error {
|
||||
return wrapUseful(err, ioErrorCode(err, errcodes.Unknown),
|
||||
"Failed to initialise the sandbox profile registry. Run with --verbose for details.")
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package sandbox
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"testing"
|
||||
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWrapUsefulPreservesContextForConvertibleErrors(t *testing.T) {
|
||||
help := "Could not create the user profile directory. Check filesystem permissions for /var/root/pmg-repro/sandbox/profiles."
|
||||
err := wrapUseful(
|
||||
fmt.Errorf("failed to create user profile directory /var/root/pmg-repro/sandbox/profiles: %w", fs.ErrPermission),
|
||||
errcodes.PermissionDenied,
|
||||
help,
|
||||
)
|
||||
|
||||
usefulErr, ok := usefulerror.AsUsefulError(err)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, errcodes.PermissionDenied, usefulErr.Code())
|
||||
assert.Equal(t, help, usefulErr.Help())
|
||||
assert.Contains(t, usefulErr.HumanError(), "failed to create user profile directory")
|
||||
}
|
||||
|
||||
func TestWrapUsefulLeavesExistingUsefulErrorsUnchanged(t *testing.T) {
|
||||
original := usefulerror.NewUsefulError().
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("already classified").
|
||||
WithHelp("existing help").
|
||||
Wrap(errors.New("root"))
|
||||
|
||||
err := wrapUseful(original, errcodes.PermissionDenied, "new help")
|
||||
|
||||
assert.Same(t, original, err)
|
||||
usefulErr, ok := usefulerror.AsUsefulError(err)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, errcodes.InvalidArgument, usefulErr.Code())
|
||||
assert.Equal(t, "existing help", usefulErr.Help())
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/pmezard/go-difflib/difflib"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/platform"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
func writeUserProfileLint(t *testing.T, dir, name, body string) string {
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
)
|
||||
|
||||
func newTestRegistry(t *testing.T, userDir string) registryFactory {
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/platform"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/ui"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
pmgsandbox "github.com/safedep/pmg/sandbox"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user