From 7777273be450ec54e8199466968fed01152636b2 Mon Sep 17 00:00:00 2001 From: Zhao vistart Date: Sun, 14 Jan 2024 01:36:36 +0800 Subject: [PATCH] - [chg]Error: switch to struct. --- workflow/simple/dag.go | 16 +++---- workflow/simple/dag_error.go | 82 ++++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 35 deletions(-) diff --git a/workflow/simple/dag.go b/workflow/simple/dag.go index 837e996..8dcfa2d 100644 --- a/workflow/simple/dag.go +++ b/workflow/simple/dag.go @@ -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} @@ -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() @@ -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 @@ -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() diff --git a/workflow/simple/dag_error.go b/workflow/simple/dag_error.go index 3eee518..40f4e81 100644 --- a/workflow/simple/dag_error.go +++ b/workflow/simple/dag_error.go @@ -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 { @@ -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 {