Created
May 12, 2018 03:36
-
-
Save russmatney/983b97b132836f06ee5245304db102bb to your computer and use it in GitHub Desktop.
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
-- StorageError in a module somewhere | |
newtype StorageError = StorageError Text deriving (Eq, Show) | |
-- WebError wraps storage Error | |
data WebError | |
= WebTextError Text | |
| WebStorageError StorageError | |
deriving (Eq, Show) | |
-- Let's convert an 'Either StorageError a' to an 'Either WebError a' | |
moreOverExamples :: IO () | |
moreOverExamples = do | |
let badStorageResponse = | |
Left (StorageError "fail!") :: Either StorageError Text | |
goodStorageResponse = | |
Right "datadata" :: Either StorageError Text | |
-- Wrap the error in WebStorageError if it's a `Left StorageError` | |
print $ over _Left (\stErr -> WebStorageError stErr) badStorageResponse | |
print $ over _Left WebStorageError badStorageResponse | |
print $ badStorageResponse & _Left %~ (\stErr -> WebStorageError stErr) | |
print $ badStorageResponse & _Left %~ WebStorageError | |
-- Left (WebStorageError (StorageError "fail!")) | |
-- This passes the 'Right Text' through just fine. | |
print $ over _Left WebStorageError goodStorageResponse | |
-- Right "datadata" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment