Skip to content

Instantly share code, notes, and snippets.

@russmatney
Created May 12, 2018 03:36
Show Gist options
  • Save russmatney/983b97b132836f06ee5245304db102bb to your computer and use it in GitHub Desktop.
Save russmatney/983b97b132836f06ee5245304db102bb to your computer and use it in GitHub Desktop.
-- 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