Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Handling Example #3

Closed
screwyprof opened this issue Sep 12, 2021 · 2 comments
Closed

Error Handling Example #3

screwyprof opened this issue Sep 12, 2021 · 2 comments
Assignees
Labels
duplicate This issue or pull request already exists ⭐️ enhancement New feature or request help wanted Extra attention is needed

Comments

@screwyprof
Copy link

screwyprof commented Sep 12, 2021

Is your feature request related to a problem? Please describe.
I'm trying to use this wonderful library in one of my project, but I'm not sure how to handle processor errors correctly.
At the moment if an errors occurs the processor.Cancel method is called, but how is it intended by the library to actually
handle these errors by the caller?

Describe the solution you'd like
There should be a simple way to return an error channel from the processor.

Describe alternatives you've considered
Alternatively we have to pass an error channel to all the processor we use in the pipeline and send an error to that channel in the process.Cancel method.

Additional context
At the moment I've created a special Result type to be used by all the processors. It has an Err and a Value fields.
But the code is very verbose:

type Result struct {
    Err error
    Value interface{}
}

func (p *SomeProcessor) Process(ctx context.Context, ins interface{}) (interface{}, error) {
	insSlice, err := toSliceOfInterfaces(ins)
	if err != nil {
		return nil, merry.Wrap(ErrCannotProcessDepositTxInput, merry.WithCause(err))
	}

	result := make([]interface{}, 0, len(insSlice))

	for _, in := range insSlice {
		res, err := ToResult(in)
		if err != nil {
			result = append(result, Result{
				Err:   errors.Wrap(ErrCannotConvertInterfaceToResult),
				Value: nil,
			})

			continue
		}

		foo, err := foo(res.Value)
		if err != nil {
			result = append(result, Result{
				Err:   errors.Wrap(ErrCannotRunFoo),
				Value: nil,
			})

			continue
		}

		bar, err := bar(ctx, foo)
		if err != nil {
			result = append(result, Result{
				Err:   errors.Wrap(ErrCannotRunBar),
				Value: nil,
			})

			continue
		}

		result = append(result, Result{Err: nil, Value: bar})
	}

	return result, nil
}

Any help or advice is appreciated.

@screwyprof screwyprof added the ⭐️ enhancement New feature or request label Sep 12, 2021
@marksalpeter marksalpeter added the help wanted Extra attention is needed label Dec 1, 2021
@marksalpeter
Copy link
Collaborator

marksalpeter commented Dec 1, 2021

Just confirming before I close this, is this a similar issue as the second part of #4?

@marksalpeter marksalpeter added duplicate This issue or pull request already exists help wanted Extra attention is needed and removed help wanted Extra attention is needed labels Dec 1, 2021
@marksalpeter
Copy link
Collaborator

Closed in favor of #4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists ⭐️ enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants