mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
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:
co-authored by
Abhisek Datta
parent
e5fe0df82e
commit
20e01d5cae
@@ -9,11 +9,12 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/safedep/pmg/usefulerror"
|
||||
"github.com/safedep/dry/usefulerror"
|
||||
"github.com/safedep/pmg/errcodes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_convertToUsefulError(t *testing.T) {
|
||||
func Test_ErrorConverters(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
inputError error
|
||||
@@ -24,60 +25,58 @@ func Test_convertToUsefulError(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "AlreadyUseful",
|
||||
inputError: usefulerror.Useful().
|
||||
inputError: usefulerror.NewUsefulError().
|
||||
WithCode("CUSTOM").
|
||||
WithHumanError("Already useful").
|
||||
Msg("test"),
|
||||
WithMsg("test"),
|
||||
wantCode: "CUSTOM",
|
||||
wantHumanError: "Already useful",
|
||||
},
|
||||
{
|
||||
name: "FileNotExist",
|
||||
inputError: &fs.PathError{Op: "open", Path: "/nonexistent/file.txt", Err: os.ErrNotExist},
|
||||
wantCode: usefulerror.ErrCodeNotFound,
|
||||
wantCode: errcodes.NotFound,
|
||||
wantContains: "/nonexistent/file.txt",
|
||||
},
|
||||
{
|
||||
name: "PermissionDenied",
|
||||
inputError: &fs.PathError{Op: "open", Path: "/root/secret", Err: os.ErrPermission},
|
||||
wantCode: usefulerror.ErrCodePermissionDenied,
|
||||
wantCode: errcodes.PermissionDenied,
|
||||
wantContains: "/root/secret",
|
||||
},
|
||||
{
|
||||
name: "ContextTimeout",
|
||||
inputError: context.DeadlineExceeded,
|
||||
wantCode: usefulerror.ErrCodeTimeout,
|
||||
wantCode: errcodes.Timeout,
|
||||
wantContains: "timed out",
|
||||
},
|
||||
{
|
||||
name: "ContextCanceled",
|
||||
inputError: context.Canceled,
|
||||
wantCode: usefulerror.ErrCodeCanceled,
|
||||
wantCode: errcodes.Canceled,
|
||||
wantContains: "canceled",
|
||||
},
|
||||
{
|
||||
name: "UnexpectedEOF",
|
||||
inputError: io.ErrUnexpectedEOF,
|
||||
wantCode: usefulerror.ErrCodeUnexpectedEOF,
|
||||
wantCode: errcodes.UnexpectedEOF,
|
||||
},
|
||||
{
|
||||
name: "WrappedError",
|
||||
inputError: fmt.Errorf("failed to read config: %w", os.ErrNotExist),
|
||||
wantCode: usefulerror.ErrCodeNotFound,
|
||||
wantCode: errcodes.NotFound,
|
||||
},
|
||||
{
|
||||
name: "UnknownError",
|
||||
inputError: errors.New("some unknown error"),
|
||||
wantCode: usefulerror.ErrCodeUnknown,
|
||||
wantHumanError: "some unknown error",
|
||||
name: "UnknownError",
|
||||
inputError: errors.New("some unknown error"),
|
||||
wantNil: true,
|
||||
},
|
||||
{
|
||||
name: "UnknownWrappedError",
|
||||
inputError: fmt.Errorf("more context: %w",
|
||||
fmt.Errorf("outer context: %w",
|
||||
errors.New("root cause error"))),
|
||||
wantCode: usefulerror.ErrCodeUnknown,
|
||||
wantHumanError: "root cause error",
|
||||
wantNil: true,
|
||||
},
|
||||
{
|
||||
name: "Nil",
|
||||
@@ -87,19 +86,20 @@ func Test_convertToUsefulError(t *testing.T) {
|
||||
{
|
||||
name: "NetworkErrorMessage",
|
||||
inputError: errors.New("connection refused"),
|
||||
wantCode: usefulerror.ErrCodeNetwork,
|
||||
wantCode: errcodes.Network,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := convertToUsefulError(tt.inputError)
|
||||
result, ok := usefulerror.AsUsefulError(tt.inputError)
|
||||
|
||||
if tt.wantNil {
|
||||
assert.Nil(t, result)
|
||||
assert.False(t, ok)
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, ok)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, tt.wantCode, result.Code())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user