sys/error: handle errors.Is for common context use-cases (#1313)

Signed-off-by: Alan Braithwaite <alan@runreveal.com>
This commit is contained in:
Alan Braithwaite
2023-03-29 20:21:19 -07:00
committed by GitHub
parent 8a9d3f2874
commit b1dd5d9000
2 changed files with 9 additions and 0 deletions

View File

@@ -73,5 +73,11 @@ func (e *ExitError) Is(err error) bool {
if target, ok := err.(*ExitError); ok {
return e.exitCode == target.exitCode
}
if e.exitCode == ExitCodeContextCanceled && err == context.Canceled {
return true
}
if e.exitCode == ExitCodeDeadlineExceeded && err == context.DeadlineExceeded {
return true
}
return false
}

View File

@@ -1,6 +1,7 @@
package sys
import (
"context"
"errors"
"testing"
@@ -55,11 +56,13 @@ func TestExitError_Error(t *testing.T) {
err := NewExitError(ExitCodeDeadlineExceeded)
require.Equal(t, ExitCodeDeadlineExceeded, err.ExitCode())
require.EqualError(t, err, "module closed with context deadline exceeded")
require.ErrorIs(t, err, context.DeadlineExceeded, "exit code context deadline exceeded should work")
})
t.Run("cancel", func(t *testing.T) {
err := NewExitError(ExitCodeContextCanceled)
require.Equal(t, ExitCodeContextCanceled, err.ExitCode())
require.EqualError(t, err, "module closed with context canceled")
require.ErrorIs(t, err, context.Canceled, "exit code context canceled should work")
})
t.Run("normal", func(t *testing.T) {
err := NewExitError(123)