-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add either.AsyncErr type & helpers (#115)
* 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
1 parent
203c319
commit f19ba3c
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |