diff --git a/common/chainable.go b/common/chainable.go index 517cea9..756b468 100644 --- a/common/chainable.go +++ b/common/chainable.go @@ -4,19 +4,22 @@ type Chainable struct { err error } +// NewChainable initialises new chainable func NewChainable() *Chainable { return &Chainable{} } -func (c *Chainable) SetChainError(err error) { +// SetErr sets chainable error +func (c *Chainable) SetErr(err error) { c.err = err } -func (c *Chainable) HasChainError() bool { +// HasErr returns true when chainable has error +func (c *Chainable) HasErr() bool { return c.err != nil } -// @TODO: rename to Err() -func (c *Chainable) ChainError() error { +// Err returns chainable error +func (c *Chainable) Err() error { return c.err } diff --git a/component/activation_result.go b/component/activation_result.go index 2c78a6e..dbdb92a 100644 --- a/component/activation_result.go +++ b/component/activation_result.go @@ -182,8 +182,8 @@ func WantsToKeepInputs(activationResult *ActivationResult) bool { return activationResult.Code() == ActivationCodeWaitingForInputsKeep } -// WithChainError returns activation result with chain error -func (ar *ActivationResult) WithChainError(err error) *ActivationResult { - ar.SetChainError(err) +// WithErr returns activation result with chain error +func (ar *ActivationResult) WithErr(err error) *ActivationResult { + ar.SetErr(err) return ar } diff --git a/component/collection.go b/component/collection.go index c663d41..daef8dc 100644 --- a/component/collection.go +++ b/component/collection.go @@ -24,14 +24,14 @@ func NewCollection() *Collection { // ByName returns a component by its name func (c *Collection) ByName(name string) *Component { - if c.HasChainError() { - return New("").WithChainError(c.ChainError()) + if c.HasErr() { + return New("").WithErr(c.Err()) } component, ok := c.components[name] if !ok { - c.SetChainError(errors.New("component not found")) + c.SetErr(errors.New("component not found")) return nil } @@ -40,24 +40,24 @@ func (c *Collection) ByName(name string) *Component { // With adds components and returns the collection func (c *Collection) With(components ...*Component) *Collection { - if c.HasChainError() { + if c.HasErr() { return c } for _, component := range components { c.components[component.Name()] = component - if component.HasChainError() { - return c.WithChainError(component.ChainError()) + if component.HasErr() { + return c.WithErr(component.Err()) } } return c } -// WithChainError returns group with error -func (c *Collection) WithChainError(err error) *Collection { - c.SetChainError(err) +// WithErr returns group with error +func (c *Collection) WithErr(err error) *Collection { + c.SetErr(err) return c } @@ -67,8 +67,8 @@ func (c *Collection) Len() int { } func (c *Collection) Components() (ComponentsMap, error) { - if c.HasChainError() { - return nil, c.ChainError() + if c.HasErr() { + return nil, c.Err() } return c.components, nil } diff --git a/component/component.go b/component/component.go index ebacc34..7828040 100644 --- a/component/component.go +++ b/component/component.go @@ -38,7 +38,7 @@ func New(name string) *Component { // WithDescription sets a description func (c *Component) WithDescription(description string) *Component { - if c.HasChainError() { + if c.HasErr() { return c } @@ -48,11 +48,11 @@ func (c *Component) WithDescription(description string) *Component { // withInputPorts sets input ports collection func (c *Component) withInputPorts(collection *port.Collection) *Component { - if c.HasChainError() { + if c.HasErr() { return c } - if collection.HasChainError() { - return c.WithChainError(collection.ChainError()) + if collection.HasErr() { + return c.WithErr(collection.Err()) } c.inputs = collection return c @@ -60,11 +60,11 @@ func (c *Component) withInputPorts(collection *port.Collection) *Component { // withOutputPorts sets input ports collection func (c *Component) withOutputPorts(collection *port.Collection) *Component { - if c.HasChainError() { + if c.HasErr() { return c } - if collection.HasChainError() { - return c.WithChainError(collection.ChainError()) + if collection.HasErr() { + return c.WithErr(collection.Err()) } c.outputs = collection @@ -73,14 +73,14 @@ func (c *Component) withOutputPorts(collection *port.Collection) *Component { // WithInputs ads input ports func (c *Component) WithInputs(portNames ...string) *Component { - if c.HasChainError() { + if c.HasErr() { return c } ports, err := port.NewGroup(portNames...).Ports() if err != nil { - c.SetChainError(err) - return New("").WithChainError(c.ChainError()) + c.SetErr(err) + return New("").WithErr(c.Err()) } return c.withInputPorts(c.Inputs().With(ports...)) @@ -88,21 +88,21 @@ func (c *Component) WithInputs(portNames ...string) *Component { // WithOutputs adds output ports func (c *Component) WithOutputs(portNames ...string) *Component { - if c.HasChainError() { + if c.HasErr() { return c } ports, err := port.NewGroup(portNames...).Ports() if err != nil { - c.SetChainError(err) - return New("").WithChainError(c.ChainError()) + c.SetErr(err) + return New("").WithErr(c.Err()) } return c.withOutputPorts(c.Outputs().With(ports...)) } // WithInputsIndexed creates multiple prefixed ports func (c *Component) WithInputsIndexed(prefix string, startIndex int, endIndex int) *Component { - if c.HasChainError() { + if c.HasErr() { return c } @@ -111,7 +111,7 @@ func (c *Component) WithInputsIndexed(prefix string, startIndex int, endIndex in // WithOutputsIndexed creates multiple prefixed ports func (c *Component) WithOutputsIndexed(prefix string, startIndex int, endIndex int) *Component { - if c.HasChainError() { + if c.HasErr() { return c } @@ -120,7 +120,7 @@ func (c *Component) WithOutputsIndexed(prefix string, startIndex int, endIndex i // WithActivationFunc sets activation function func (c *Component) WithActivationFunc(f ActivationFunc) *Component { - if c.HasChainError() { + if c.HasErr() { return c } @@ -130,7 +130,7 @@ func (c *Component) WithActivationFunc(f ActivationFunc) *Component { // WithLabels sets labels and returns the component func (c *Component) WithLabels(labels common.LabelsCollection) *Component { - if c.HasChainError() { + if c.HasErr() { return c } c.LabeledEntity.SetLabels(labels) @@ -139,8 +139,8 @@ func (c *Component) WithLabels(labels common.LabelsCollection) *Component { // Inputs getter func (c *Component) Inputs() *port.Collection { - if c.HasChainError() { - return port.NewCollection().WithChainError(c.ChainError()) + if c.HasErr() { + return port.NewCollection().WithErr(c.Err()) } return c.inputs @@ -148,8 +148,8 @@ func (c *Component) Inputs() *port.Collection { // Outputs getter func (c *Component) Outputs() *port.Collection { - if c.HasChainError() { - return port.NewCollection().WithChainError(c.ChainError()) + if c.HasErr() { + return port.NewCollection().WithErr(c.Err()) } return c.outputs @@ -157,33 +157,33 @@ func (c *Component) Outputs() *port.Collection { // OutputByName is shortcut method func (c *Component) OutputByName(name string) *port.Port { - if c.HasChainError() { - return port.New("").WithChainError(c.ChainError()) + if c.HasErr() { + return port.New("").WithErr(c.Err()) } outputPort := c.Outputs().ByName(name) - if outputPort.HasChainError() { - c.SetChainError(outputPort.ChainError()) - return port.New("").WithChainError(c.ChainError()) + if outputPort.HasErr() { + c.SetErr(outputPort.Err()) + return port.New("").WithErr(c.Err()) } return outputPort } // InputByName is shortcut method func (c *Component) InputByName(name string) *port.Port { - if c.HasChainError() { - return port.New("").WithChainError(c.ChainError()) + if c.HasErr() { + return port.New("").WithErr(c.Err()) } inputPort := c.Inputs().ByName(name) - if inputPort.HasChainError() { - c.SetChainError(inputPort.ChainError()) - return port.New("").WithChainError(c.ChainError()) + if inputPort.HasErr() { + c.SetErr(inputPort.Err()) + return port.New("").WithErr(c.Err()) } return inputPort } // hasActivationFunction checks when activation function is set func (c *Component) hasActivationFunction() bool { - if c.HasChainError() { + if c.HasErr() { return false } @@ -192,26 +192,26 @@ func (c *Component) hasActivationFunction() bool { // propagateChainErrors propagates up all chain errors that might have not been propagated yet func (c *Component) propagateChainErrors() { - if c.Inputs().HasChainError() { - c.SetChainError(c.Inputs().ChainError()) + if c.Inputs().HasErr() { + c.SetErr(c.Inputs().Err()) return } - if c.Outputs().HasChainError() { - c.SetChainError(c.Outputs().ChainError()) + if c.Outputs().HasErr() { + c.SetErr(c.Outputs().Err()) return } for _, p := range c.Inputs().PortsOrNil() { - if p.HasChainError() { - c.SetChainError(p.ChainError()) + if p.HasErr() { + c.SetErr(p.Err()) return } } for _, p := range c.Outputs().PortsOrNil() { - if p.HasChainError() { - c.SetChainError(p.ChainError()) + if p.HasErr() { + c.SetErr(p.Err()) return } } @@ -223,8 +223,8 @@ func (c *Component) propagateChainErrors() { func (c *Component) MaybeActivate() (activationResult *ActivationResult) { c.propagateChainErrors() - if c.HasChainError() { - activationResult = NewActivationResult(c.Name()).WithChainError(c.ChainError()) + if c.HasErr() { + activationResult = NewActivationResult(c.Name()).WithErr(c.Err()) return } @@ -265,19 +265,19 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) { // FlushOutputs pushed signals out of the component outputs to pipes and clears outputs func (c *Component) FlushOutputs() *Component { - if c.HasChainError() { + if c.HasErr() { return c } ports, err := c.Outputs().Ports() if err != nil { - c.SetChainError(err) - return New("").WithChainError(c.ChainError()) + c.SetErr(err) + return New("").WithErr(c.Err()) } for _, out := range ports { out = out.Flush() - if out.HasChainError() { - return c.WithChainError(out.ChainError()) + if out.HasErr() { + return c.WithErr(out.Err()) } } return c @@ -285,15 +285,15 @@ func (c *Component) FlushOutputs() *Component { // ClearInputs clears all input ports func (c *Component) ClearInputs() *Component { - if c.HasChainError() { + if c.HasErr() { return c } c.Inputs().Clear() return c } -// WithChainError returns component with error -func (c *Component) WithChainError(err error) *Component { - c.SetChainError(err) +// WithErr returns component with error +func (c *Component) WithErr(err error) *Component { + c.SetErr(err) return c } diff --git a/component/component_test.go b/component/component_test.go index 26f781d..b7c4e22 100644 --- a/component/component_test.go +++ b/component/component_test.go @@ -92,7 +92,7 @@ func TestComponent_FlushOutputs(t *testing.T) { name: "with chain error", getComponent: func() *Component { sink := port.New("sink") - c := New("c").WithOutputs("o1").WithChainError(errors.New("some error")) + c := New("c").WithOutputs("o1").WithErr(errors.New("some error")) //Lines below are ignored as error immediately propagates up to component level c.Outputs().ByName("o1").PipeTo(sink) c.Outputs().ByName("o1").PutSignals(signal.New("signal from component with chain error")) @@ -527,23 +527,23 @@ func TestComponent_MaybeActivate(t *testing.T) { name: "with chain error from input port", getComponent: func() *Component { c := New("c").WithInputs("i1").WithOutputs("o1") - c.Inputs().With(port.New("p").WithChainError(errors.New("some error"))) + c.Inputs().With(port.New("p").WithErr(errors.New("some error"))) return c }, wantActivationResult: NewActivationResult("c"). WithActivationCode(ActivationCodeUndefined). - WithChainError(errors.New("some error")), + WithErr(errors.New("some error")), }, { name: "with chain error from output port", getComponent: func() *Component { c := New("c").WithInputs("i1").WithOutputs("o1") - c.Outputs().With(port.New("p").WithChainError(errors.New("some error"))) + c.Outputs().With(port.New("p").WithErr(errors.New("some error"))) return c }, wantActivationResult: NewActivationResult("c"). WithActivationCode(ActivationCodeUndefined). - WithChainError(errors.New("some error")), + WithErr(errors.New("some error")), }, } for _, tt := range tests { diff --git a/cycle/cycle.go b/cycle/cycle.go index eaf75d2..7051b76 100644 --- a/cycle/cycle.go +++ b/cycle/cycle.go @@ -59,8 +59,8 @@ func (cycle *Cycle) WithNumber(number int) *Cycle { return cycle } -// WithChainError returns cycle with error -func (cycle *Cycle) WithChainError(err error) *Cycle { - cycle.SetChainError(err) +// WithErr returns cycle with error +func (cycle *Cycle) WithErr(err error) *Cycle { + cycle.SetErr(err) return cycle } diff --git a/cycle/cycle_test.go b/cycle/cycle_test.go index c2e052b..6d6d82f 100644 --- a/cycle/cycle_test.go +++ b/cycle/cycle_test.go @@ -11,7 +11,7 @@ func TestNew(t *testing.T) { t.Run("happy path", func(t *testing.T) { cycle := New() assert.NotNil(t, cycle) - assert.False(t, cycle.HasChainError()) + assert.False(t, cycle.HasErr()) }) } diff --git a/cycle/group.go b/cycle/group.go index 5aba75c..a668c4e 100644 --- a/cycle/group.go +++ b/cycle/group.go @@ -37,8 +37,8 @@ func (g *Group) withCycles(cycles Cycles) *Group { // Cycles getter func (g *Group) Cycles() (Cycles, error) { - if g.HasChainError() { - return nil, g.ChainError() + if g.HasErr() { + return nil, g.Err() } return g.cycles, nil } diff --git a/fmesh.go b/fmesh.go index ec4f9b8..1ba982f 100644 --- a/fmesh.go +++ b/fmesh.go @@ -45,15 +45,15 @@ func New(name string) *FMesh { // Components getter func (fm *FMesh) Components() *component.Collection { - if fm.HasChainError() { - return component.NewCollection().WithChainError(fm.ChainError()) + if fm.HasErr() { + return component.NewCollection().WithErr(fm.Err()) } return fm.components } // WithDescription sets a description func (fm *FMesh) WithDescription(description string) *FMesh { - if fm.HasChainError() { + if fm.HasErr() { return fm } @@ -63,14 +63,14 @@ func (fm *FMesh) WithDescription(description string) *FMesh { // WithComponents adds components to f-mesh func (fm *FMesh) WithComponents(components ...*component.Component) *FMesh { - if fm.HasChainError() { + if fm.HasErr() { return fm } for _, c := range components { fm.components = fm.components.With(c) - if c.HasChainError() { - return fm.WithChainError(c.ChainError()) + if c.HasErr() { + return fm.WithErr(c.Err()) } } return fm @@ -78,7 +78,7 @@ func (fm *FMesh) WithComponents(components ...*component.Component) *FMesh { // WithConfig sets the configuration and returns the f-mesh func (fm *FMesh) WithConfig(config Config) *FMesh { - if fm.HasChainError() { + if fm.HasErr() { return fm } @@ -90,26 +90,26 @@ func (fm *FMesh) WithConfig(config Config) *FMesh { func (fm *FMesh) runCycle() *cycle.Cycle { newCycle := cycle.New() - if fm.HasChainError() { - return newCycle.WithChainError(fm.ChainError()) + if fm.HasErr() { + return newCycle.WithErr(fm.Err()) } if fm.Components().Len() == 0 { - fm.SetChainError(errors.New("failed to run cycle: no components found")) - return newCycle.WithChainError(fm.ChainError()) + fm.SetErr(errors.New("failed to run cycle: no components found")) + return newCycle.WithErr(fm.Err()) } var wg sync.WaitGroup components, err := fm.Components().Components() if err != nil { - fm.SetChainError(fmt.Errorf("failed to run cycle: %w", err)) - return newCycle.WithChainError(fm.ChainError()) + fm.SetErr(fmt.Errorf("failed to run cycle: %w", err)) + return newCycle.WithErr(fm.Err()) } for _, c := range components { - if c.HasChainError() { - fm.SetChainError(c.ChainError()) + if c.HasErr() { + fm.SetErr(c.Err()) } wg.Add(1) @@ -126,8 +126,8 @@ func (fm *FMesh) runCycle() *cycle.Cycle { //Bubble up chain errors from activation results for _, ar := range newCycle.ActivationResults() { - if ar.HasChainError() { - newCycle.SetChainError(ar.ChainError()) + if ar.HasErr() { + newCycle.SetErr(ar.Err()) break } } @@ -137,8 +137,8 @@ func (fm *FMesh) runCycle() *cycle.Cycle { // DrainComponents drains the data from activated components func (fm *FMesh) drainComponents(cycle *cycle.Cycle) error { - if fm.HasChainError() { - return fm.ChainError() + if fm.HasErr() { + return fm.Err() } components, err := fm.Components().Components() @@ -149,8 +149,8 @@ func (fm *FMesh) drainComponents(cycle *cycle.Cycle) error { for _, c := range components { activationResult := cycle.ActivationResults().ByComponentName(c.Name()) - if activationResult.HasChainError() { - return activationResult.ChainError() + if activationResult.HasErr() { + return activationResult.Err() } if !activationResult.Activated() { @@ -183,8 +183,8 @@ func (fm *FMesh) drainComponents(cycle *cycle.Cycle) error { // Run starts the computation until there is no component which activates (mesh has no unprocessed inputs) func (fm *FMesh) Run() (cycle.Cycles, error) { - if fm.HasChainError() { - return nil, fm.ChainError() + if fm.HasErr() { + return nil, fm.Err() } allCycles := cycle.NewGroup() @@ -192,9 +192,9 @@ func (fm *FMesh) Run() (cycle.Cycles, error) { for { cycleResult := fm.runCycle().WithNumber(cycleNumber) - if cycleResult.HasChainError() { - fm.SetChainError(cycleResult.ChainError()) - return nil, fmt.Errorf("chain error occurred in cycle #%d : %w", cycleResult.Number(), cycleResult.ChainError()) + if cycleResult.HasErr() { + fm.SetErr(cycleResult.Err()) + return nil, fmt.Errorf("chain error occurred in cycle #%d : %w", cycleResult.Number(), cycleResult.Err()) } allCycles = allCycles.With(cycleResult) @@ -222,8 +222,8 @@ func (fm *FMesh) Run() (cycle.Cycles, error) { // mustStop defines when f-mesh must stop after activation cycle func (fm *FMesh) mustStop(cycleResult *cycle.Cycle) (bool, error, error) { - if fm.HasChainError() { - return false, fm.ChainError(), nil + if fm.HasErr() { + return false, fm.Err(), nil } if (fm.config.CyclesLimit > 0) && (cycleResult.Number() > fm.config.CyclesLimit) { @@ -254,8 +254,8 @@ func (fm *FMesh) mustStop(cycleResult *cycle.Cycle) (bool, error, error) { } } -// WithChainError returns f-mesh with error -func (fm *FMesh) WithChainError(err error) *FMesh { - fm.SetChainError(err) +// WithErr returns f-mesh with error +func (fm *FMesh) WithErr(err error) *FMesh { + fm.SetErr(err) return fm } diff --git a/fmesh_test.go b/fmesh_test.go index 0791d25..d8f6224 100644 --- a/fmesh_test.go +++ b/fmesh_test.go @@ -651,11 +651,11 @@ func TestFMesh_runCycle(t *testing.T) { } cycleResult := tt.fm.runCycle() if tt.wantError { - assert.True(t, cycleResult.HasChainError()) - assert.Error(t, cycleResult.ChainError()) + assert.True(t, cycleResult.HasErr()) + assert.Error(t, cycleResult.Err()) } else { - assert.False(t, cycleResult.HasChainError()) - assert.NoError(t, cycleResult.ChainError()) + assert.False(t, cycleResult.HasErr()) + assert.NoError(t, cycleResult.Err()) assert.Equal(t, tt.want, cycleResult) } }) diff --git a/integration_tests/error_handling/chainable_api_test.go b/integration_tests/error_handling/chainable_api_test.go index 24880ba..36d60c2 100644 --- a/integration_tests/error_handling/chainable_api_test.go +++ b/integration_tests/error_handling/chainable_api_test.go @@ -20,14 +20,14 @@ func Test_Signal(t *testing.T) { test: func(t *testing.T) { sig := signal.New(123) _, err := sig.Payload() - assert.False(t, sig.HasChainError()) + assert.False(t, sig.HasErr()) assert.NoError(t, err) _ = sig.PayloadOrDefault(555) - assert.False(t, sig.HasChainError()) + assert.False(t, sig.HasErr()) _ = sig.PayloadOrNil() - assert.False(t, sig.HasChainError()) + assert.False(t, sig.HasErr()) }, }, { @@ -36,8 +36,8 @@ func Test_Signal(t *testing.T) { emptyGroup := signal.NewGroup() sig := emptyGroup.First() - assert.True(t, sig.HasChainError()) - assert.Error(t, sig.ChainError()) + assert.True(t, sig.HasErr()) + assert.Error(t, sig.Err()) _, err := sig.Payload() assert.Error(t, err) @@ -72,7 +72,7 @@ func Test_FMesh(t *testing.T) { fm.Components().ByName("c1").InputByName("num2").PutSignals(signal.New(5)) _, err := fm.Run() - assert.False(t, fm.HasChainError()) + assert.False(t, fm.HasErr()) assert.NoError(t, err) }, }, @@ -89,14 +89,14 @@ func Test_FMesh(t *testing.T) { outputs.ByName("sum").PutSignals(signal.New(num1 + num2)) return nil }). - WithChainError(errors.New("some error in component")), + WithErr(errors.New("some error in component")), ) fm.Components().ByName("c1").InputByName("num1").PutSignals(signal.New(10)) fm.Components().ByName("c1").InputByName("num2").PutSignals(signal.New(5)) _, err := fm.Run() - assert.True(t, fm.HasChainError()) + assert.True(t, fm.HasErr()) assert.Error(t, err) assert.EqualError(t, err, "some error in component") }, @@ -121,7 +121,7 @@ func Test_FMesh(t *testing.T) { fm.Components().ByName("c1").InputByName("num2").PutSignals(signal.New(5)) _, err := fm.Run() - assert.True(t, fm.HasChainError()) + assert.True(t, fm.HasErr()) assert.Error(t, err) assert.EqualError(t, err, "chain error occurred in cycle #0 : port not found") }, @@ -139,11 +139,11 @@ func Test_FMesh(t *testing.T) { }), ) - fm.Components().ByName("c1").InputByName("num1").PutSignals(signal.New(10).WithChainError(errors.New("some error in input signal"))) + fm.Components().ByName("c1").InputByName("num1").PutSignals(signal.New(10).WithErr(errors.New("some error in input signal"))) fm.Components().ByName("c1").InputByName("num2").PutSignals(signal.New(5)) _, err := fm.Run() - assert.True(t, fm.HasChainError()) + assert.True(t, fm.HasErr()) assert.Error(t, err) assert.EqualError(t, err, "chain error occurred in cycle #0 : some error in input signal") }, diff --git a/port/collection.go b/port/collection.go index 5541d82..02f0f2d 100644 --- a/port/collection.go +++ b/port/collection.go @@ -29,21 +29,21 @@ func NewCollection() *Collection { // ByName returns a port by its name func (collection *Collection) ByName(name string) *Port { - if collection.HasChainError() { - return New("").WithChainError(collection.ChainError()) + if collection.HasErr() { + return New("").WithErr(collection.Err()) } port, ok := collection.ports[name] if !ok { - collection.SetChainError(ErrPortNotFoundInCollection) - return New("").WithChainError(collection.ChainError()) + collection.SetErr(ErrPortNotFoundInCollection) + return New("").WithErr(collection.Err()) } return port } // ByNames returns multiple ports by their names func (collection *Collection) ByNames(names ...string) *Collection { - if collection.HasChainError() { - return NewCollection().WithChainError(collection.ChainError()) + if collection.HasErr() { + return NewCollection().WithErr(collection.Err()) } //Preserve collection config @@ -60,7 +60,7 @@ func (collection *Collection) ByNames(names ...string) *Collection { // AnyHasSignals returns true if at least one port in collection has signals func (collection *Collection) AnyHasSignals() bool { - if collection.HasChainError() { + if collection.HasErr() { return false } @@ -75,7 +75,7 @@ func (collection *Collection) AnyHasSignals() bool { // AllHaveSignals returns true when all ports in collection have signals func (collection *Collection) AllHaveSignals() bool { - if collection.HasChainError() { + if collection.HasErr() { return false } @@ -90,14 +90,14 @@ func (collection *Collection) AllHaveSignals() bool { // PutSignals adds buffer to every port in collection func (collection *Collection) PutSignals(signals ...*signal.Signal) *Collection { - if collection.HasChainError() { - return NewCollection().WithChainError(collection.ChainError()) + if collection.HasErr() { + return NewCollection().WithErr(collection.Err()) } for _, p := range collection.ports { p.PutSignals(signals...) - if p.HasChainError() { - return collection.WithChainError(p.ChainError()) + if p.HasErr() { + return collection.WithErr(p.Err()) } } @@ -109,8 +109,8 @@ func (collection *Collection) Clear() *Collection { for _, p := range collection.ports { p.Clear() - if p.HasChainError() { - return collection.WithChainError(p.ChainError()) + if p.HasErr() { + return collection.WithErr(p.Err()) } } return collection @@ -118,15 +118,15 @@ func (collection *Collection) Clear() *Collection { // Flush flushes all ports in collection func (collection *Collection) Flush() *Collection { - if collection.HasChainError() { - return NewCollection().WithChainError(collection.ChainError()) + if collection.HasErr() { + return NewCollection().WithErr(collection.Err()) } for _, p := range collection.ports { p = p.Flush() - if p.HasChainError() { - return collection.WithChainError(p.ChainError()) + if p.HasErr() { + return collection.WithErr(p.Err()) } } return collection @@ -137,8 +137,8 @@ func (collection *Collection) PipeTo(destPorts ...*Port) *Collection { for _, p := range collection.ports { p = p.PipeTo(destPorts...) - if p.HasChainError() { - return collection.WithChainError(p.ChainError()) + if p.HasErr() { + return collection.WithErr(p.Err()) } } @@ -147,13 +147,13 @@ func (collection *Collection) PipeTo(destPorts ...*Port) *Collection { // With adds ports to collection and returns it func (collection *Collection) With(ports ...*Port) *Collection { - if collection.HasChainError() { + if collection.HasErr() { return collection } for _, port := range ports { - if port.HasChainError() { - return collection.WithChainError(port.ChainError()) + if port.HasErr() { + return collection.WithErr(port.Err()) } port.AddLabels(collection.defaultLabels) collection.ports[port.Name()] = port @@ -164,30 +164,30 @@ func (collection *Collection) With(ports ...*Port) *Collection { // WithIndexed creates ports with names like "o1","o2","o3" and so on func (collection *Collection) WithIndexed(prefix string, startIndex int, endIndex int) *Collection { - if collection.HasChainError() { + if collection.HasErr() { return collection } indexedPorts, err := NewIndexedGroup(prefix, startIndex, endIndex).Ports() if err != nil { - collection.SetChainError(err) - return NewCollection().WithChainError(collection.ChainError()) + collection.SetErr(err) + return NewCollection().WithErr(collection.Err()) } return collection.With(indexedPorts...) } // Signals returns all signals of all ports in the collection func (collection *Collection) Signals() *signal.Group { - if collection.HasChainError() { - return signal.NewGroup().WithChainError(collection.ChainError()) + if collection.HasErr() { + return signal.NewGroup().WithErr(collection.Err()) } group := signal.NewGroup() for _, p := range collection.ports { signals, err := p.Buffer().Signals() if err != nil { - collection.SetChainError(err) - return signal.NewGroup().WithChainError(collection.ChainError()) + collection.SetErr(err) + return signal.NewGroup().WithErr(collection.Err()) } group = group.With(signals...) } @@ -197,8 +197,8 @@ func (collection *Collection) Signals() *signal.Group { // Ports getter // @TODO:maybe better to hide all errors within chainable and ask user to check error ? func (collection *Collection) Ports() (PortMap, error) { - if collection.HasChainError() { - return nil, collection.ChainError() + if collection.HasErr() { + return nil, collection.Err() } return collection.ports, nil } @@ -210,7 +210,7 @@ func (collection *Collection) PortsOrNil() PortMap { // PortsOrDefault returns ports or default in case of any error func (collection *Collection) PortsOrDefault(defaultPorts PortMap) PortMap { - if collection.HasChainError() { + if collection.HasErr() { return defaultPorts } @@ -221,9 +221,9 @@ func (collection *Collection) PortsOrDefault(defaultPorts PortMap) PortMap { return ports } -// WithChainError returns group with error -func (collection *Collection) WithChainError(err error) *Collection { - collection.SetChainError(err) +// WithErr returns group with error +func (collection *Collection) WithErr(err error) *Collection { + collection.SetErr(err) return collection } diff --git a/port/collection_test.go b/port/collection_test.go index 63ffe0d..fd4ebdc 100644 --- a/port/collection_test.go +++ b/port/collection_test.go @@ -107,15 +107,15 @@ func TestCollection_ByName(t *testing.T) { args: args{ name: "p3", }, - want: New("").WithChainError(ErrPortNotFoundInCollection), + want: New("").WithErr(ErrPortNotFoundInCollection), }, { name: "with chain error", - collection: NewCollection().With(NewGroup("p1", "p2").PortsOrNil()...).WithChainError(errors.New("some error")), + collection: NewCollection().With(NewGroup("p1", "p2").PortsOrNil()...).WithErr(errors.New("some error")), args: args{ name: "p1", }, - want: New("").WithChainError(errors.New("some error")), + want: New("").WithErr(errors.New("some error")), }, } for _, tt := range tests { @@ -170,11 +170,11 @@ func TestCollection_ByNames(t *testing.T) { }, { name: "with chain error", - collection: NewCollection().With(NewGroup("p1", "p2").PortsOrNil()...).WithChainError(errors.New("some error")), + collection: NewCollection().With(NewGroup("p1", "p2").PortsOrNil()...).WithErr(errors.New("some error")), args: args{ names: []string{"p1", "p2"}, }, - want: NewCollection().WithChainError(errors.New("some error")), + want: NewCollection().WithErr(errors.New("some error")), }, } for _, tt := range tests { diff --git a/port/group.go b/port/group.go index 3949126..58080f1 100644 --- a/port/group.go +++ b/port/group.go @@ -31,7 +31,7 @@ func NewGroup(names ...string) *Group { // NOTE: endIndex is inclusive, e.g. NewIndexedGroup("p", 0, 0) will create one port with name "p0" func NewIndexedGroup(prefix string, startIndex int, endIndex int) *Group { if startIndex > endIndex { - return NewGroup().WithChainError(ErrInvalidRangeForIndexedGroup) + return NewGroup().WithErr(ErrInvalidRangeForIndexedGroup) } ports := make(Ports, endIndex-startIndex+1) @@ -45,7 +45,7 @@ func NewIndexedGroup(prefix string, startIndex int, endIndex int) *Group { // With adds ports to group func (g *Group) With(ports ...*Port) *Group { - if g.HasChainError() { + if g.HasErr() { return g } @@ -66,8 +66,8 @@ func (g *Group) withPorts(ports Ports) *Group { // Ports getter func (g *Group) Ports() (Ports, error) { - if g.HasChainError() { - return nil, g.ChainError() + if g.HasErr() { + return nil, g.Err() } return g.ports, nil } @@ -86,9 +86,9 @@ func (g *Group) PortsOrDefault(defaultPorts Ports) Ports { return ports } -// WithChainError returns group with error -func (g *Group) WithChainError(err error) *Group { - g.SetChainError(err) +// WithErr returns group with error +func (g *Group) WithErr(err error) *Group { + g.SetErr(err) return g } diff --git a/port/group_test.go b/port/group_test.go index 36026e2..a50ee82 100644 --- a/port/group_test.go +++ b/port/group_test.go @@ -80,7 +80,7 @@ func TestNewIndexedGroup(t *testing.T) { startIndex: 999, endIndex: 5, }, - want: NewGroup().WithChainError(ErrInvalidRangeForIndexedGroup), + want: NewGroup().WithErr(ErrInvalidRangeForIndexedGroup), }, } for _, tt := range tests { diff --git a/port/port.go b/port/port.go index b2f2aa5..8186d59 100644 --- a/port/port.go +++ b/port/port.go @@ -36,8 +36,8 @@ func New(name string) *Port { // Buffer getter // @TODO: maybe we can hide this and return signals to user code func (p *Port) Buffer() *signal.Group { - if p.HasChainError() { - return signal.NewGroup().WithChainError(p.ChainError()) + if p.HasErr() { + return signal.NewGroup().WithErr(p.Err()) } return p.buffer } @@ -45,21 +45,21 @@ func (p *Port) Buffer() *signal.Group { // Pipes getter // @TODO maybe better to return []*Port directly func (p *Port) Pipes() *Group { - if p.HasChainError() { - return NewGroup().WithChainError(p.ChainError()) + if p.HasErr() { + return NewGroup().WithErr(p.Err()) } return p.pipes } // withBuffer sets buffer field func (p *Port) withBuffer(buffer *signal.Group) *Port { - if p.HasChainError() { + if p.HasErr() { return p } - if buffer.HasChainError() { - p.SetChainError(buffer.ChainError()) - return New("").WithChainError(p.ChainError()) + if buffer.HasErr() { + p.SetErr(buffer.Err()) + return New("").WithErr(p.Err()) } p.buffer = buffer return p @@ -68,7 +68,7 @@ func (p *Port) withBuffer(buffer *signal.Group) *Port { // PutSignals adds signals to buffer // @TODO: rename func (p *Port) PutSignals(signals ...*signal.Signal) *Port { - if p.HasChainError() { + if p.HasErr() { return p } return p.withBuffer(p.Buffer().With(signals...)) @@ -76,7 +76,7 @@ func (p *Port) PutSignals(signals ...*signal.Signal) *Port { // WithSignals puts buffer and returns the port func (p *Port) WithSignals(signals ...*signal.Signal) *Port { - if p.HasChainError() { + if p.HasErr() { return p } @@ -85,18 +85,18 @@ func (p *Port) WithSignals(signals ...*signal.Signal) *Port { // WithSignalGroups puts groups of buffer and returns the port func (p *Port) WithSignalGroups(signalGroups ...*signal.Group) *Port { - if p.HasChainError() { + if p.HasErr() { return p } for _, group := range signalGroups { signals, err := group.Signals() if err != nil { - p.SetChainError(err) - return New("").WithChainError(p.ChainError()) + p.SetErr(err) + return New("").WithErr(p.Err()) } p.PutSignals(signals...) - if p.HasChainError() { - return New("").WithChainError(p.ChainError()) + if p.HasErr() { + return New("").WithErr(p.Err()) } } @@ -105,7 +105,7 @@ func (p *Port) WithSignalGroups(signalGroups ...*signal.Group) *Port { // Clear removes all signals from the port buffer func (p *Port) Clear() *Port { - if p.HasChainError() { + if p.HasErr() { return p } return p.withBuffer(signal.NewGroup()) @@ -114,7 +114,7 @@ func (p *Port) Clear() *Port { // Flush pushes buffer to pipes and clears the port // @TODO: hide this method from user func (p *Port) Flush() *Port { - if p.HasChainError() { + if p.HasErr() { return p } @@ -126,16 +126,16 @@ func (p *Port) Flush() *Port { pipes, err := p.pipes.Ports() if err != nil { - p.SetChainError(err) - return New("").WithChainError(p.ChainError()) + p.SetErr(err) + return New("").WithErr(p.Err()) } for _, outboundPort := range pipes { //Fan-Out err = ForwardSignals(p, outboundPort) if err != nil { - p.SetChainError(err) - return New("").WithChainError(p.ChainError()) + p.SetErr(err) + return New("").WithErr(p.Err()) } } return p.Clear() @@ -154,14 +154,14 @@ func (p *Port) HasPipes() bool { // PipeTo creates one or multiple pipes to other port(s) // @TODO: hide this method from AF func (p *Port) PipeTo(destPorts ...*Port) *Port { - if p.HasChainError() { + if p.HasErr() { return p } for _, destPort := range destPorts { if err := validatePipe(p, destPort); err != nil { - p.SetChainError(fmt.Errorf("pipe validation failed: %w", err)) - return New("").WithChainError(p.ChainError()) + p.SetErr(fmt.Errorf("pipe validation failed: %w", err)) + return New("").WithErr(p.Err()) } p.pipes = p.pipes.With(destPort) } @@ -188,7 +188,7 @@ func validatePipe(srcPort *Port, dstPort *Port) error { // WithLabels sets labels and returns the port func (p *Port) WithLabels(labels common.LabelsCollection) *Port { - if p.HasChainError() { + if p.HasErr() { return p } @@ -203,15 +203,15 @@ func ForwardSignals(source *Port, dest *Port) error { return err } dest.PutSignals(signals...) - if dest.HasChainError() { - return dest.ChainError() + if dest.HasErr() { + return dest.Err() } return nil } -// WithChainError returns port with error -func (p *Port) WithChainError(err error) *Port { - p.SetChainError(err) +// WithErr returns port with error +func (p *Port) WithErr(err error) *Port { + p.SetErr(err) return p } diff --git a/port/port_test.go b/port/port_test.go index f503a4c..e11bd31 100644 --- a/port/port_test.go +++ b/port/port_test.go @@ -50,8 +50,8 @@ func TestPort_Buffer(t *testing.T) { }, { name: "with chain error", - port: New("p").WithChainError(errors.New("some error")), - want: signal.NewGroup().WithChainError(errors.New("some error")), + port: New("p").WithErr(errors.New("some error")), + want: signal.NewGroup().WithErr(errors.New("some error")), }, } for _, tt := range tests { @@ -118,8 +118,8 @@ func TestPort_PipeTo(t *testing.T) { toPorts: Ports{inputPorts.ByName("in2"), inputPorts.ByName("in3")}, }, assertions: func(t *testing.T, portAfter *Port) { - assert.False(t, portAfter.HasChainError()) - assert.NoError(t, portAfter.ChainError()) + assert.False(t, portAfter.HasErr()) + assert.NoError(t, portAfter.Err()) assert.Equal(t, 2, portAfter.Pipes().Len()) }, }, @@ -131,8 +131,8 @@ func TestPort_PipeTo(t *testing.T) { }, assertions: func(t *testing.T, portAfter *Port) { assert.Equal(t, "", portAfter.Name()) - assert.True(t, portAfter.HasChainError()) - assert.Error(t, portAfter.ChainError()) + assert.True(t, portAfter.HasErr()) + assert.Error(t, portAfter.Err()) }, }, { @@ -143,8 +143,8 @@ func TestPort_PipeTo(t *testing.T) { }, assertions: func(t *testing.T, portAfter *Port) { assert.Equal(t, "", portAfter.Name()) - assert.True(t, portAfter.HasChainError()) - assert.Error(t, portAfter.ChainError()) + assert.True(t, portAfter.HasErr()) + assert.Error(t, portAfter.Err()) }, }, { @@ -157,8 +157,8 @@ func TestPort_PipeTo(t *testing.T) { }, assertions: func(t *testing.T, portAfter *Port) { assert.Equal(t, "", portAfter.Name()) - assert.True(t, portAfter.HasChainError()) - assert.Error(t, portAfter.ChainError()) + assert.True(t, portAfter.HasErr()) + assert.Error(t, portAfter.Err()) }, }, { @@ -169,8 +169,8 @@ func TestPort_PipeTo(t *testing.T) { }, assertions: func(t *testing.T, portAfter *Port) { assert.Equal(t, "", portAfter.Name()) - assert.True(t, portAfter.HasChainError()) - assert.Error(t, portAfter.ChainError()) + assert.True(t, portAfter.HasErr()) + assert.Error(t, portAfter.Err()) }, }, } @@ -249,20 +249,20 @@ func TestPort_PutSignals(t *testing.T) { port: New("p"), assertions: func(t *testing.T, portAfter *Port) { assert.Zero(t, portAfter.Buffer().Len()) - assert.True(t, portAfter.Buffer().HasChainError()) + assert.True(t, portAfter.Buffer().HasErr()) }, args: args{ - signals: signal.Signals{signal.New(111).WithChainError(errors.New("some error in signal"))}, + signals: signal.Signals{signal.New(111).WithErr(errors.New("some error in signal"))}, }, }, { name: "with chain error", - port: New("p").WithChainError(errors.New("some error in port")), + port: New("p").WithErr(errors.New("some error in port")), args: args{ signals: signal.Signals{signal.New(123)}, }, assertions: func(t *testing.T, portAfter *Port) { - assert.True(t, portAfter.HasChainError()) + assert.True(t, portAfter.HasErr()) assert.Zero(t, portAfter.Buffer().Len()) }, }, @@ -501,8 +501,8 @@ func TestPort_Pipes(t *testing.T) { }, { name: "with chain error", - port: New("p").WithChainError(errors.New("some error")), - want: NewGroup().WithChainError(errors.New("some error")), + port: New("p").WithErr(errors.New("some error")), + want: NewGroup().WithErr(errors.New("some error")), }, } for _, tt := range tests { @@ -521,22 +521,22 @@ func TestPort_ShortcutGetters(t *testing.T) { }) t.Run("FirstSignalPayloadOrNil", func(t *testing.T) { - port := New("p").WithSignals(signal.New(123).WithChainError(errors.New("some error"))) + port := New("p").WithSignals(signal.New(123).WithErr(errors.New("some error"))) assert.Nil(t, port.FirstSignalPayloadOrNil()) }) t.Run("FirstSignalPayloadOrDefault", func(t *testing.T) { - port := New("p").WithSignals(signal.New(123).WithChainError(errors.New("some error"))) + port := New("p").WithSignals(signal.New(123).WithErr(errors.New("some error"))) assert.Equal(t, 888, port.FirstSignalPayloadOrDefault(888)) }) t.Run("AllSignalsOrNil", func(t *testing.T) { - port := New("p").WithSignals(signal.New(123).WithChainError(errors.New("some error"))) + port := New("p").WithSignals(signal.New(123).WithErr(errors.New("some error"))) assert.Nil(t, port.AllSignalsOrNil()) }) t.Run("AllSignalsOrDefault", func(t *testing.T) { - port := New("p").WithSignals(signal.New(123).WithChainError(errors.New("some error"))) + port := New("p").WithSignals(signal.New(123).WithErr(errors.New("some error"))) assert.Equal(t, signal.NewGroup(999).SignalsOrNil(), port.AllSignalsOrDefault(signal.NewGroup(999).SignalsOrNil())) }) } diff --git a/signal/group.go b/signal/group.go index e3fe2c3..86e2f5e 100644 --- a/signal/group.go +++ b/signal/group.go @@ -27,13 +27,13 @@ func NewGroup(payloads ...any) *Group { // First returns the first signal in the group func (g *Group) First() *Signal { - if g.HasChainError() { - return New(nil).WithChainError(g.ChainError()) + if g.HasErr() { + return New(nil).WithErr(g.Err()) } if len(g.signals) == 0 { - g.SetChainError(ErrNoSignalsInGroup) - return New(nil).WithChainError(g.ChainError()) + g.SetErr(ErrNoSignalsInGroup) + return New(nil).WithErr(g.Err()) } return g.signals[0] @@ -41,8 +41,8 @@ func (g *Group) First() *Signal { // FirstPayload returns the first signal payload func (g *Group) FirstPayload() (any, error) { - if g.HasChainError() { - return nil, g.ChainError() + if g.HasErr() { + return nil, g.Err() } return g.First().Payload() @@ -50,8 +50,8 @@ func (g *Group) FirstPayload() (any, error) { // AllPayloads returns a slice with all payloads of the all signals in the group func (g *Group) AllPayloads() ([]any, error) { - if g.HasChainError() { - return nil, g.ChainError() + if g.HasErr() { + return nil, g.Err() } all := make([]any, len(g.signals)) @@ -67,7 +67,7 @@ func (g *Group) AllPayloads() ([]any, error) { // With returns the group with added signals func (g *Group) With(signals ...*Signal) *Group { - if g.HasChainError() { + if g.HasErr() { // Do nothing, but propagate error return g } @@ -76,13 +76,13 @@ func (g *Group) With(signals ...*Signal) *Group { copy(newSignals, g.signals) for i, sig := range signals { if sig == nil { - g.SetChainError(ErrInvalidSignal) - return NewGroup().WithChainError(g.ChainError()) + g.SetErr(ErrInvalidSignal) + return NewGroup().WithErr(g.Err()) } - if sig.HasChainError() { - g.SetChainError(sig.ChainError()) - return NewGroup().WithChainError(g.ChainError()) + if sig.HasErr() { + g.SetErr(sig.Err()) + return NewGroup().WithErr(g.Err()) } newSignals[len(g.signals)+i] = sig @@ -93,7 +93,7 @@ func (g *Group) With(signals ...*Signal) *Group { // WithPayloads returns a group with added signals created from provided payloads func (g *Group) WithPayloads(payloads ...any) *Group { - if g.HasChainError() { + if g.HasErr() { // Do nothing, but propagate error return g } @@ -114,8 +114,8 @@ func (g *Group) withSignals(signals Signals) *Group { // Signals getter func (g *Group) Signals() (Signals, error) { - if g.HasChainError() { - return nil, g.ChainError() + if g.HasErr() { + return nil, g.Err() } return g.signals, nil } @@ -134,9 +134,9 @@ func (g *Group) SignalsOrDefault(defaultSignals Signals) Signals { return signals } -// WithChainError returns group with error -func (g *Group) WithChainError(err error) *Group { - g.SetChainError(err) +// WithErr returns group with error +func (g *Group) WithErr(err error) *Group { + g.SetErr(err) return g } diff --git a/signal/group_test.go b/signal/group_test.go index 585cf7b..6d9ef5b 100644 --- a/signal/group_test.go +++ b/signal/group_test.go @@ -77,7 +77,7 @@ func TestGroup_FirstPayload(t *testing.T) { }, { name: "with error in chain", - group: NewGroup(3, 4, 5).WithChainError(errors.New("some error in chain")), + group: NewGroup(3, 4, 5).WithErr(errors.New("some error in chain")), want: nil, wantErrorString: "some error in chain", }, @@ -114,13 +114,13 @@ func TestGroup_AllPayloads(t *testing.T) { }, { name: "with error in chain", - group: NewGroup(1, 2, 3).WithChainError(errors.New("some error in chain")), + group: NewGroup(1, 2, 3).WithErr(errors.New("some error in chain")), want: nil, wantErrorString: "some error in chain", }, { name: "with error in signal", - group: NewGroup().withSignals(Signals{New(33).WithChainError(errors.New("some error in signal"))}), + group: NewGroup().withSignals(Signals{New(33).WithErr(errors.New("some error in signal"))}), want: nil, wantErrorString: "some error in signal", }, @@ -189,28 +189,28 @@ func TestGroup_With(t *testing.T) { args: args{ signals: NewGroup(7, nil, 9).SignalsOrNil(), }, - want: NewGroup().WithChainError(errors.New("signal is invalid")), + want: NewGroup().WithErr(errors.New("signal is invalid")), }, { name: "with error in signal", - group: NewGroup(1, 2, 3).With(New(44).WithChainError(errors.New("some error in signal"))), + group: NewGroup(1, 2, 3).With(New(44).WithErr(errors.New("some error in signal"))), args: args{ signals: Signals{New(456)}, }, want: NewGroup(1, 2, 3). With(New(44). - WithChainError(errors.New("some error in signal"))). - WithChainError(errors.New("some error in signal")), // error propagated from signal to group + WithErr(errors.New("some error in signal"))). + WithErr(errors.New("some error in signal")), // error propagated from signal to group }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.group.With(tt.args.signals...) - if tt.want.HasChainError() { - assert.Error(t, got.ChainError()) - assert.EqualError(t, got.ChainError(), tt.want.ChainError().Error()) + if tt.want.HasErr() { + assert.Error(t, got.Err()) + assert.EqualError(t, got.Err(), tt.want.Err().Error()) } else { - assert.NoError(t, got.ChainError()) + assert.NoError(t, got.Err()) } assert.Equal(t, tt.want, got) }) @@ -276,7 +276,7 @@ func TestGroup_First(t *testing.T) { { name: "empty group", group: NewGroup(), - want: New(nil).WithChainError(errors.New("group has no signals")), + want: New(nil).WithErr(errors.New("group has no signals")), }, { name: "happy path", @@ -285,17 +285,17 @@ func TestGroup_First(t *testing.T) { }, { name: "with error in chain", - group: NewGroup(1, 2, 3).WithChainError(errors.New("some error in chain")), - want: New(nil).WithChainError(errors.New("some error in chain")), + group: NewGroup(1, 2, 3).WithErr(errors.New("some error in chain")), + want: New(nil).WithErr(errors.New("some error in chain")), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.group.First() - if tt.want.HasChainError() { - assert.True(t, got.HasChainError()) - assert.Error(t, got.ChainError()) - assert.EqualError(t, got.ChainError(), tt.want.ChainError().Error()) + if tt.want.HasErr() { + assert.True(t, got.HasErr()) + assert.Error(t, got.Err()) + assert.EqualError(t, got.Err(), tt.want.Err().Error()) } else { assert.Equal(t, tt.want, tt.group.First()) } @@ -324,7 +324,7 @@ func TestGroup_Signals(t *testing.T) { }, { name: "with error in chain", - group: NewGroup(1, 2, 3).WithChainError(errors.New("some error in chain")), + group: NewGroup(1, 2, 3).WithErr(errors.New("some error in chain")), want: nil, wantErrorString: "some error in chain", }, @@ -370,7 +370,7 @@ func TestGroup_SignalsOrDefault(t *testing.T) { }, { name: "with error in chain and nil default", - group: NewGroup(1, 2, 3).WithChainError(errors.New("some error in chain")), + group: NewGroup(1, 2, 3).WithErr(errors.New("some error in chain")), args: args{ defaultSignals: nil, }, @@ -378,7 +378,7 @@ func TestGroup_SignalsOrDefault(t *testing.T) { }, { name: "with error in chain and default", - group: NewGroup(1, 2, 3).WithChainError(errors.New("some error in chain")), + group: NewGroup(1, 2, 3).WithErr(errors.New("some error in chain")), args: args{ defaultSignals: Signals{New(4), New(5)}, }, diff --git a/signal/signal.go b/signal/signal.go index 44bec3a..f33b475 100644 --- a/signal/signal.go +++ b/signal/signal.go @@ -18,8 +18,8 @@ func New(payload any) *Signal { // Payload getter func (s *Signal) Payload() (any, error) { - if s.HasChainError() { - return nil, s.ChainError() + if s.HasErr() { + return nil, s.Err() } return s.payload[0], nil } @@ -38,8 +38,8 @@ func (s *Signal) PayloadOrDefault(defaultPayload any) any { return payload } -// WithChainError returns signal with error -func (s *Signal) WithChainError(err error) *Signal { - s.SetChainError(err) +// WithErr returns signal with error +func (s *Signal) WithErr(err error) *Signal { + s.SetErr(err) return s } diff --git a/signal/signal_test.go b/signal/signal_test.go index 8d38ffb..0bccc01 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -63,7 +63,7 @@ func TestSignal_Payload(t *testing.T) { }, { name: "with error in chain", - signal: New(123).WithChainError(errors.New("some error in chain")), + signal: New(123).WithErr(errors.New("some error in chain")), want: nil, wantErrorString: "some error in chain", }, @@ -95,7 +95,7 @@ func TestSignal_PayloadOrNil(t *testing.T) { }, { name: "nil returned", - signal: New(123).WithChainError(errors.New("some error in chain")), + signal: New(123).WithErr(errors.New("some error in chain")), want: nil, }, }