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:
@@ -10,12 +10,13 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/safedep/dry/log"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/safedep/pmg/internal/pty"
|
||||
"github.com/safedep/pmg/internal/shim"
|
||||
"github.com/safedep/pmg/packagemanager"
|
||||
"github.com/safedep/pmg/sandbox"
|
||||
"github.com/safedep/pmg/sandbox/executor"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
)
|
||||
|
||||
type ExecutionMode int
|
||||
@@ -141,6 +142,7 @@ func runPTY(
|
||||
if !result.ShouldRun() {
|
||||
return usefulerror.NewUsefulError().
|
||||
Wrap(fmt.Errorf("sandbox not supported for PTY sessions")).
|
||||
WithCode(errcodes.InvalidArgument).
|
||||
WithHumanError("Sandbox executed command cannot be used with PTY session. Please use non-interactive TTY mode instead.")
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ func Test_ErrorConverters(t *testing.T) {
|
||||
inputError: fmt.Errorf("more context: %w",
|
||||
fmt.Errorf("outer context: %w",
|
||||
errors.New("root cause error"))),
|
||||
wantNil: true,
|
||||
wantNil: true,
|
||||
},
|
||||
{
|
||||
name: "Nil",
|
||||
@@ -114,6 +114,65 @@ func Test_ErrorConverters(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_convertToUsefulError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
inputError error
|
||||
wantCode string
|
||||
wantHumanError string
|
||||
wantNil bool
|
||||
}{
|
||||
{
|
||||
name: "Nil",
|
||||
inputError: nil,
|
||||
wantNil: true,
|
||||
},
|
||||
{
|
||||
name: "AlreadyUseful",
|
||||
inputError: usefulerror.NewUsefulError().
|
||||
WithCode("CUSTOM").
|
||||
WithHumanError("Already useful"),
|
||||
wantCode: "CUSTOM",
|
||||
wantHumanError: "Already useful",
|
||||
},
|
||||
{
|
||||
name: "Converted",
|
||||
inputError: &fs.PathError{Op: "open", Path: "/nonexistent/file.txt", Err: os.ErrNotExist},
|
||||
wantCode: errcodes.NotFound,
|
||||
wantHumanError: "File or directory not found: /nonexistent/file.txt",
|
||||
},
|
||||
{
|
||||
name: "UnknownFallsBackToRootCause",
|
||||
inputError: errors.New("some unknown error"),
|
||||
wantCode: errcodes.Unknown,
|
||||
wantHumanError: "some unknown error",
|
||||
},
|
||||
{
|
||||
name: "UnknownWrappedExtractsRootCause",
|
||||
inputError: fmt.Errorf("more context: %w",
|
||||
fmt.Errorf("outer context: %w",
|
||||
errors.New("root cause error"))),
|
||||
wantCode: errcodes.Unknown,
|
||||
wantHumanError: "root cause error",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := convertToUsefulError(tt.inputError)
|
||||
|
||||
if tt.wantNil {
|
||||
assert.Nil(t, result)
|
||||
return
|
||||
}
|
||||
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, tt.wantCode, result.Code())
|
||||
assert.Equal(t, tt.wantHumanError, result.HumanError())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractPathFromError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user