Skip to content

Commit

Permalink
return []error rather than error
Browse files Browse the repository at this point in the history
Concurrent functions return multiple errors.
These are now returned only as either a channel or a slice.

This avoids a dependency on a 3rd party multi errors library.
The standard library errors.Join is awkward to use because
there is not a simple way to unwrap the errors.

We could use a custom multi error.
However, to make this convenient to use it should be returned as a
pointer to the struct rather than an opaque error.
But Go has a subtle design issue where
an interface can end up being non-nil when storing a pointer to a struct.

Returning []error still allows the caller to use errors.Join() if they don't
care about iterating the individual errors.

The main issue is that someone may check != nil rather than len > 0
However, all the APIs are designed to return nil rather than a zero
length slice.

Additionally, don't use the default recovery handler.
Write more errors to the error channels.
Panic a few errors that shouldn't happen.
  • Loading branch information
Greg Weber committed Oct 29, 2024
1 parent 352789c commit 2cf6934
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 237 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ parallelism in Go using generics

## Error handling

This library relies on go-recovery to trap panics that occur in go routines.
go-recovery by default will log panics but can be configured to send them to an error monitoring service.
This library relies on go-recovery to trap panics that occur in user supplied work functions.
This library does have unhandled panics, but only in places where panics should never occur.
Errors and panics are written to an error channel for maximum flexibility.
There are helpers for common patterns for dealing with errors:

For maximum flexibility with error handling, many of the parallel functions return an error channel.
Any errors that occur in work functions will be put into the error channel.
There are helpers for common patterns for dealing with the errors:

* CollectErrors
* CancelAfterFirstError
* CollectErrors (wait and convert to a slice)
* CancelAfterFirstError (cancel and wait and convert to a slice)
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ go 1.19
require (
github.com/gregwebs/go-recovery v0.2.1
github.com/stretchr/testify v1.8.1
go.uber.org/multierr v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gregwebs/errors v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
Loading

0 comments on commit 2cf6934

Please sign in to comment.