Skip to content

Commit

Permalink
- [chg]Error: switch to struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
vistart committed Jan 13, 2024
1 parent b07366d commit 7777273
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
16 changes: 8 additions & 8 deletions workflow/simple/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ func (d *Channels) exists(name string) bool {
// and the specified channel exists, otherwise an error will be reported.
func (d *Channels) get(name string) (chan any, error) {
if d == nil {
return nil, ErrChannelNotInitialized
return nil, ErrChannelNotInitialized{}
}
// d.muChannels.RLock()
//defer d.muChannels.RUnlock()
if d.channels == nil {
return nil, ErrChannelNotInitialized
return nil, ErrChannelNotInitialized{}
}
if _, existed := d.channels[name]; !existed {
return nil, ErrChannelNotExist{name: name}
Expand All @@ -187,7 +187,7 @@ func (d *Channels) get(name string) (chan any, error) {
// Note that the channel name to be added cannot already exist. Otherwise, `ErrChannelNameExisted` will be returned.
func (d *Channels) add(names ...string) error {
if d == nil {
return ErrChannelNotInitialized
return ErrChannelNotInitialized{}
}
d.muChannels.Lock()
defer d.muChannels.Unlock()
Expand Down Expand Up @@ -389,9 +389,9 @@ func (d *DAG[TInput, TOutput]) BuildWorkflowOutput(ctx context.Context, outputs
//
// - ErrChannelNotInitialized if channels is nil
//
// - ErrChannelInputEmpty if channelInput is empty
// - ErrChannelInputNotSpecified if channelInput is empty
//
// - ErrChannelOutputEmpty if channelOutput is empty
// - ErrChannelOutputNotSpecified if channelOutput is empty
//
// If the transits is empty, do nothing and return nil directly.
// Otherwise, it is determined whether the input and output channel names mentioned in each transit are defined
Expand All @@ -408,19 +408,19 @@ func (d *DAG[TInput, TOutput]) BuildWorkflow(ctx context.Context) error {
d.muChannels.RLock()
defer d.muChannels.RUnlock()
if d.channels == nil || d.channels.channels == nil {
return ErrChannelNotInitialized
return ErrChannelNotInitialized{}
}

d.channels.muChannels.RLock()
defer d.channels.muChannels.RUnlock()
// Checks the channel Input
if len(d.channels.channelInput) == 0 {
return ErrChannelInputEmpty
return ErrChannelInputNotSpecified{}
}

// Checks the channel Output
if len(d.channels.channelOutput) == 0 {
return ErrChannelOutputEmpty
return ErrChannelOutputNotSpecified{}
}

d.transits.muTransits.RLock()
Expand Down
82 changes: 55 additions & 27 deletions workflow/simple/dag_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,94 @@
package simple

import (
"errors"
"fmt"
"reflect"
"strings"
)

// ErrChannelNotInitialized reports when the channel list is not initialized.
var ErrChannelNotInitialized = errors.New("the channel map is not initialized")
// ErrChannelInterface indicates errors reported by the check channel.
type ErrChannelInterface interface {
error
}

// ErrChannelNotInitialized indicates that the channel map is not instantiated.
type ErrChannelNotInitialized struct {
ErrChannelInterface
}

func (e ErrChannelNotInitialized) Error() string {
return "channel map not initialized"
}

// ErrChannelNotExist indicates that the specified channel does not exist.
type ErrChannelNotExist struct {
ErrChannelInterface
name string
error
}

func (e ErrChannelNotExist) Error() string {
return fmt.Sprintf("channel[%s] not exist", e.name)
}

// ErrChannelInputEmpty indicates that the input channel is empty.
var ErrChannelInputEmpty = errors.New("the input channel is empty")
// ErrChannelInputNotSpecified indicates that the input channel is not specified.
type ErrChannelInputNotSpecified struct {
ErrChannelInterface
}

// ErrChannelOutputEmpty indicates that the output channel is empty.
var ErrChannelOutputEmpty = errors.New("the output channel is empty")
func (e ErrChannelInputNotSpecified) Error() string {
return "channel input not specified"
}

// ErrWorkerPanicked reports when the worker is panicked.
type ErrWorkerPanicked struct {
panic any
error
// ErrChannelOutputNotSpecified indicates that the output channel is not specified.
type ErrChannelOutputNotSpecified struct {
ErrChannelInterface
}

func (e ErrWorkerPanicked) Error() string {
return fmt.Sprintf("worker panicked.")
func (e ErrChannelOutputNotSpecified) Error() string {
return "channel output not specified"
}

// ErrChannelNameExisted indicates that the specified channel has existed.
type ErrChannelNameExisted struct {
ErrChannelInterface
name string
error
}

func (e ErrChannelNameExisted) Error() string {
return fmt.Sprintf("the channel[%s] has existed.", e.name)
}

// ErrRedundantChannels indicates that there are unused channelInputs.
type ErrRedundantChannels struct {
ErrChannelInterface
channels []string
}

func (e ErrRedundantChannels) Error() string {
return fmt.Sprintf("Redundant channelInputs: %v", strings.Join(e.channels, ", "))
}

// ErrTransitInterface represents the error reported by transit.
type ErrTransitInterface interface {
error
}

// ErrWorkerPanicked reports when the worker is panicked.
type ErrWorkerPanicked struct {
ErrTransitInterface
panic any
}

func (e ErrWorkerPanicked) Error() string {
return fmt.Sprintf("worker panicked.")
}

// ErrValueTypeMismatch defines that the data type output by the node is inconsistent with expectation.
type ErrValueTypeMismatch struct {
ErrTransitInterface
expect any
actual any
input string
error
}

func (e ErrValueTypeMismatch) Error() string {
Expand All @@ -69,22 +106,13 @@ func NewErrValueTypeMismatch(expect, actual any, input string) ErrValueTypeMisma
return ErrValueTypeMismatch{expect: expect, actual: actual, input: input}
}

// ErrRedundantChannels indicates that there are unused channelInputs.
type ErrRedundantChannels struct {
channels []string
error
}

func (e ErrRedundantChannels) Error() string {
return fmt.Sprintf("Redundant channelInputs: %v", strings.Join(e.channels, ", "))
}

// ErrTransitChannelNonExist indicates that the channel(s) to be used by the specified node does not exist.
type ErrTransitChannelNonExist struct {
ErrChannelInterface
ErrTransitInterface
transitName string
channelInputs []string
channelOutputs []string
error
}

func (e ErrTransitChannelNonExist) Error() string {
Expand Down

0 comments on commit 7777273

Please sign in to comment.