Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions workflow/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func WithCancelCause(parent Context) (ctx Context, cancel CancelCauseFunc) {
return sync.WithCancelCause(parent)
}

// Cause returns a non-nil error explaining why c was canceled.
// The first cancellation of c or one of its parents sets the cause.
// If that cancellation happened via a call to CancelCauseFunc(err),
// then [Cause] returns err.
// Otherwise Cause(c) returns the same value as c.Err().
// Cause returns nil if c has not been canceled yet.
func Cause(c Context) error {
return sync.Cause(c)
}

// WithValue returns a copy of parent in which the value associated with key is
// val.
//
Expand Down
21 changes: 21 additions & 0 deletions workflow/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package workflow

import (
"errors"
"testing"

"github.com/cschleiden/go-workflows/internal/sync"
"github.com/stretchr/testify/require"
)

func TestCause(t *testing.T) {
myErr := errors.New("my cause")

ctx, cancel := WithCancelCause(sync.Background())
require.NoError(t, Cause(ctx))

cancel(myErr)

require.ErrorIs(t, ctx.Err(), Canceled)
require.Equal(t, myErr, Cause(ctx))
}
Loading