Skip to content

Commit

Permalink
Chainable API: polish error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Nov 4, 2024
1 parent abb0e00 commit 96d41ef
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
1 change: 1 addition & 0 deletions common/chainable.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (c *Chainable) HasChainError() bool {
return c.err != nil
}

// @TODO: rename to Err()

Check notice on line 19 in common/chainable.go

View workflow job for this annotation

GitHub Actions / qodana

Comment of exported element starts with the incorrect name

Comment should have the following format 'ChainError ...' (with an optional leading article)
func (c *Chainable) ChainError() error {
return c.err
}
18 changes: 14 additions & 4 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func (c *Component) WithDescription(description string) *Component {

// withInputPorts sets input ports collection
func (c *Component) withInputPorts(collection *port.Collection) *Component {
if c.HasChainError() {
return c
}
if collection.HasChainError() {
return c.WithChainError(collection.ChainError())
}
Expand All @@ -57,6 +60,9 @@ func (c *Component) withInputPorts(collection *port.Collection) *Component {

// withOutputPorts sets input ports collection
func (c *Component) withOutputPorts(collection *port.Collection) *Component {
if c.HasChainError() {
return c
}
if collection.HasChainError() {
return c.WithChainError(collection.ChainError())
}
Expand All @@ -73,8 +79,10 @@ func (c *Component) WithInputs(portNames ...string) *Component {

ports, err := port.NewGroup(portNames...).Ports()
if err != nil {
return c.WithChainError(err)
c.SetChainError(err)
return New("").WithChainError(c.ChainError())
}

return c.withInputPorts(c.Inputs().With(ports...))
}

Expand All @@ -86,7 +94,8 @@ func (c *Component) WithOutputs(portNames ...string) *Component {

ports, err := port.NewGroup(portNames...).Ports()
if err != nil {
return c.WithChainError(err)
c.SetChainError(err)
return New("").WithChainError(c.ChainError())
}
return c.withOutputPorts(c.Outputs().With(ports...))
}
Expand Down Expand Up @@ -262,10 +271,11 @@ func (c *Component) FlushOutputs() *Component {

ports, err := c.Outputs().Ports()
if err != nil {
return c.WithChainError(err)
c.SetChainError(err)
return New("").WithChainError(c.ChainError())
}
for _, out := range ports {
out.Flush()
out = out.Flush()
if out.HasChainError() {
return c.WithChainError(out.ChainError())
}
Expand Down
6 changes: 4 additions & 2 deletions fmesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ func (fm *FMesh) runCycle() *cycle.Cycle {
}

if fm.Components().Len() == 0 {
return newCycle.WithChainError(errors.New("failed to run cycle: no components found"))
fm.SetChainError(errors.New("failed to run cycle: no components found"))
return newCycle.WithChainError(fm.ChainError())
}

var wg sync.WaitGroup

components, err := fm.Components().Components()
if err != nil {
return newCycle.WithChainError(fmt.Errorf("failed to run cycle: %w", err))
fm.SetChainError(fmt.Errorf("failed to run cycle: %w", err))
return newCycle.WithChainError(fm.ChainError())
}

for _, c := range components {
Expand Down
13 changes: 8 additions & 5 deletions port/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (collection *Collection) ByNames(names ...string) *Collection {
return NewCollection().WithChainError(collection.ChainError())
}

//Preserve collection config
selectedPorts := NewCollection().WithDefaultLabels(collection.defaultLabels)

for _, name := range names {
Expand Down Expand Up @@ -122,7 +123,7 @@ func (collection *Collection) Flush() *Collection {
}

for _, p := range collection.ports {
p.Flush()
p = p.Flush()

if p.HasChainError() {
return collection.WithChainError(p.ChainError())
Expand All @@ -147,7 +148,7 @@ 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() {
return NewCollection().WithChainError(collection.ChainError())
return collection
}

for _, port := range ports {
Expand All @@ -164,12 +165,13 @@ 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() {
return NewCollection().WithChainError(collection.ChainError())
return collection
}

indexedPorts, err := NewIndexedGroup(prefix, startIndex, endIndex).Ports()
if err != nil {
return collection.WithChainError(err)
collection.SetChainError(err)
return NewCollection().WithChainError(collection.ChainError())
}
return collection.With(indexedPorts...)
}
Expand All @@ -184,7 +186,8 @@ func (collection *Collection) Signals() *signal.Group {
for _, p := range collection.ports {
signals, err := p.Buffer().Signals()
if err != nil {
return group.WithChainError(err)
collection.SetChainError(err)
return signal.NewGroup().WithChainError(collection.ChainError())
}
group = group.With(signals...)
}
Expand Down
22 changes: 15 additions & 7 deletions port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func New(name string) *Port {
// @TODO: maybe we can hide this and return signals to user code
func (p *Port) Buffer() *signal.Group {
if p.HasChainError() {
return p.buffer.WithChainError(p.ChainError())
return signal.NewGroup().WithChainError(p.ChainError())
}
return p.buffer
}
Expand All @@ -46,15 +46,20 @@ func (p *Port) Buffer() *signal.Group {
// @TODO maybe better to return []*Port directly
func (p *Port) Pipes() *Group {
if p.HasChainError() {
return p.pipes.WithChainError(p.ChainError())
return NewGroup().WithChainError(p.ChainError())
}
return p.pipes
}

// withBuffer sets buffer field
func (p *Port) withBuffer(buffer *signal.Group) *Port {
if p.HasChainError() {
return p
}

if buffer.HasChainError() {
return p.WithChainError(buffer.ChainError())
p.SetChainError(buffer.ChainError())
return New("").WithChainError(p.ChainError())
}
p.buffer = buffer
return p
Expand Down Expand Up @@ -86,11 +91,12 @@ func (p *Port) WithSignalGroups(signalGroups ...*signal.Group) *Port {
for _, group := range signalGroups {
signals, err := group.Signals()
if err != nil {
return p.WithChainError(err)
p.SetChainError(err)
return New("").WithChainError(p.ChainError())
}
p.PutSignals(signals...)
if p.HasChainError() {
return p
return New("").WithChainError(p.ChainError())
}
}

Expand Down Expand Up @@ -120,14 +126,16 @@ func (p *Port) Flush() *Port {

pipes, err := p.pipes.Ports()
if err != nil {
return p.WithChainError(err)
p.SetChainError(err)
return New("").WithChainError(p.ChainError())
}

for _, outboundPort := range pipes {
//Fan-Out
err = ForwardSignals(p, outboundPort)
if err != nil {
return p.WithChainError(err)
p.SetChainError(err)
return New("").WithChainError(p.ChainError())
}
}
return p.Clear()
Expand Down
8 changes: 5 additions & 3 deletions signal/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (g *Group) First() *Signal {

if len(g.signals) == 0 {
g.SetChainError(ErrNoSignalsInGroup)
return New(nil).WithChainError(ErrNoSignalsInGroup)
return New(nil).WithChainError(g.ChainError())
}

return g.signals[0]
Expand Down Expand Up @@ -76,11 +76,13 @@ func (g *Group) With(signals ...*Signal) *Group {
copy(newSignals, g.signals)
for i, sig := range signals {
if sig == nil {
return g.WithChainError(ErrInvalidSignal)
g.SetChainError(ErrInvalidSignal)
return NewGroup().WithChainError(g.ChainError())
}

if sig.HasChainError() {
return g.WithChainError(sig.ChainError())
g.SetChainError(sig.ChainError())
return NewGroup().WithChainError(g.ChainError())
}

newSignals[len(g.signals)+i] = sig
Expand Down
2 changes: 1 addition & 1 deletion signal/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestGroup_With(t *testing.T) {
args: args{
signals: NewGroup(7, nil, 9).SignalsOrNil(),
},
want: NewGroup(1, 2, 3, "valid before invalid").WithChainError(errors.New("signal is invalid")),
want: NewGroup().WithChainError(errors.New("signal is invalid")),
},
{
name: "with error in signal",
Expand Down

0 comments on commit 96d41ef

Please sign in to comment.