Skip to content

Commit

Permalink
Merge pull request #322 from pace/util
Browse files Browse the repository at this point in the history
errors: add helper to check for context errors
  • Loading branch information
alessandroMiceli committed Sep 5, 2022
2 parents e706ab8 + 0a81895 commit ce321c7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
9 changes: 8 additions & 1 deletion maintenance/errors/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func Hide(ctx context.Context, err, exposedErr error) error {
if err == nil {
return nil
}

ret := errors.New(err.Error())
if exposedErr != nil {
ret = fmt.Errorf("%w: %s", exposedErr, err)
Expand All @@ -36,3 +36,10 @@ func Hide(ctx context.Context, err, exposedErr error) error {

return ret
}

func IsStdLibContextError(err error) bool {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return true
}
return false
}
38 changes: 25 additions & 13 deletions maintenance/errors/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ func TestHide(t *testing.T) {
exposedErr error
}
tests := []struct {
name string
args args
want error
name string
args args
want error
expectContextErr bool
}{
{
name: "normal_context_no_error_nothing_exposed",
Expand All @@ -57,7 +58,8 @@ func TestHide(t *testing.T) {
err: fmt.Errorf("%s: %w", iAmAnError, context.Canceled),
exposedErr: nil,
},
want: fmt.Errorf("%s: %s", iAmAnError, context.Canceled),
want: fmt.Errorf("%s: %s", iAmAnError, context.Canceled),
expectContextErr: true,
},
{
name: "normal_context_with_error_with_exposed",
Expand All @@ -66,7 +68,8 @@ func TestHide(t *testing.T) {
err: fmt.Errorf("%s: %w", iAmAnError, context.Canceled),
exposedErr: iAmAnotherError,
},
want: fmt.Errorf("%w: %s: %s", iAmAnotherError, iAmAnError, context.Canceled),
want: fmt.Errorf("%w: %s: %s", iAmAnotherError, iAmAnError, context.Canceled),
expectContextErr: true,
},
{
name: "canceled_context_no_error_nothing_exposed",
Expand All @@ -75,7 +78,8 @@ func TestHide(t *testing.T) {
err: iAmAnError,
exposedErr: nil,
},
want: iAmAnError,
want: iAmAnError,
expectContextErr: false,
},
{
name: "canceled_context_no_error_with_exposed",
Expand All @@ -84,7 +88,8 @@ func TestHide(t *testing.T) {
err: iAmAnError,
exposedErr: iAmAnotherError,
},
want: fmt.Errorf("%w: %s", iAmAnotherError, iAmAnError),
want: fmt.Errorf("%w: %s", iAmAnotherError, iAmAnError),
expectContextErr: false,
},
{
name: "canceled_context_with_error_nothing_exposed",
Expand All @@ -93,7 +98,8 @@ func TestHide(t *testing.T) {
err: fmt.Errorf("%s: %w", iAmAnError, context.Canceled),
exposedErr: nil,
},
want: fmt.Errorf("%s: %w", iAmAnError, context.Canceled),
want: fmt.Errorf("%s: %w", iAmAnError, context.Canceled),
expectContextErr: true,
},
{
name: "canceled_context_with_error_with_exposed",
Expand All @@ -102,7 +108,8 @@ func TestHide(t *testing.T) {
err: fmt.Errorf("%s: %w", iAmAnError, context.Canceled),
exposedErr: iAmAnotherError,
},
want: fmt.Errorf("%s: %s: %w", iAmAnotherError, iAmAnError, context.Canceled),
want: fmt.Errorf("%s: %s: %w", iAmAnotherError, iAmAnError, context.Canceled),
expectContextErr: true,
},
{
name: "exceeded_context_no_error_nothing_exposed",
Expand All @@ -111,7 +118,8 @@ func TestHide(t *testing.T) {
err: iAmAnError,
exposedErr: nil,
},
want: iAmAnError,
want: iAmAnError,
expectContextErr: false,
},
{
name: "exceeded_context_no_error_with_exposed",
Expand All @@ -120,7 +128,8 @@ func TestHide(t *testing.T) {
err: iAmAnError,
exposedErr: iAmAnotherError,
},
want: fmt.Errorf("%w: %s", iAmAnotherError, iAmAnError),
want: fmt.Errorf("%w: %s", iAmAnotherError, iAmAnError),
expectContextErr: false,
},
{
name: "exceeded_context_with_error_nothing_exposed",
Expand All @@ -129,7 +138,8 @@ func TestHide(t *testing.T) {
err: fmt.Errorf("%s: %w", iAmAnError, context.DeadlineExceeded),
exposedErr: nil,
},
want: fmt.Errorf("%s: %w", iAmAnError, context.DeadlineExceeded),
want: fmt.Errorf("%s: %w", iAmAnError, context.DeadlineExceeded),
expectContextErr: true,
},
{
name: "exceeded_context_with_error_with_exposed",
Expand All @@ -138,13 +148,15 @@ func TestHide(t *testing.T) {
err: fmt.Errorf("%s: %w", iAmAnError, context.DeadlineExceeded),
exposedErr: iAmAnotherError,
},
want: fmt.Errorf("%s: %s: %w", iAmAnotherError, iAmAnError, context.DeadlineExceeded),
want: fmt.Errorf("%s: %s: %w", iAmAnotherError, iAmAnError, context.DeadlineExceeded),
expectContextErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Hide(tt.args.ctx, tt.args.err, tt.args.exposedErr)
require.Equal(t, tt.want, got)
require.Equal(t, tt.expectContextErr, IsStdLibContextError(tt.args.err))
})
}
}

0 comments on commit ce321c7

Please sign in to comment.