Skip to content

Commit

Permalink
feat: add either.AsyncErr type & helpers (#115)
Browse files Browse the repository at this point in the history
* feat: add either.AsyncErr type & helpers

* fix: comment typo

Co-authored-by: Daniel Olshansky <[email protected]>

---------

Co-authored-by: Daniel Olshansky <[email protected]>
  • Loading branch information
bryanchriswhite and Olshansk committed Oct 31, 2023
1 parent 203c319 commit f19ba3c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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]

0 comments on commit f19ba3c

Please sign in to comment.