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

feat: add either.AsyncErr type & helpers #115

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pkg/either/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package either

// SyncErr creates an AsyncError either from a synchronous error.
// It wraps the Error into the left field (conventionally associated with the
// error value in the Either pattern) of the Either type. It casts the result
// to the AsyncError type.
func SyncErr(err error) AsyncError {
return AsyncError(Error[chan error](err))
}

// AsyncErr creates an AsyncError from an error channel.
// It wraps the error channel into the right field (conventionally associated with
// successful values in the Either pattern) of the Either type.
func AsyncErr(errCh chan error) AsyncError {
return AsyncError(Success[chan error](errCh))
}

// SyncOrAsyncError decomposes the AsyncError into its components, returning
// a synchronous error and an error channel. If the AsyncError represents a
// synchronous error, the error channel will be nil and vice versa.
func (soaErr AsyncError) SyncOrAsyncError() (error, chan error) {
errCh, err := Either[chan error](soaErr).ValueOrError()
return err, errCh
}

// IsSyncError checks if the AsyncError represents a synchronous error.
func (soaErr AsyncError) IsSyncError() bool {
return Either[chan error](soaErr).IsError()
}

// IsAsyncError checks if the AsyncError represents an asynchronous error
// (sent through a channel).
func (soaErr AsyncError) IsAsyncError() bool {
return Either[chan error](soaErr).IsSuccess()
}
6 changes: 6 additions & 0 deletions pkg/either/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package either

// AsyncError represents a value which could either be a synchronous error or
// an asynchronous error (sent through a channel). It wraps the more generic
// `Either` type specific for error channels.
type AsyncError Either[chan error]