Created
July 26, 2011 13:58
-
-
Save jb55/1106824 to your computer and use it in GitHub Desktop.
Handy error handling combinators
This file contains hidden or 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
| data Error = Timeout | |
| | Exception SomeException |
This file contains hidden or 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
| import Control.Exception | |
| import System.Timeout | |
| liftST :: Int -> (a -> IO b) -> a -> IO (Either Error b) | |
| liftST n f = liftM conv . try . timeout n . f | |
| where | |
| conv :: Either SomeException (Maybe a) -> Either Error a | |
| conv (Left e) = Left (Exception e) | |
| conv (Right Nothing) = Left Timeout | |
| conv (Right (Just r)) = Right r |
This file contains hidden or 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
| import Network.HTTP | |
| type HttpResponse = Network.Stream.Result (Response String) | |
| dl :: String -> IO HttpResponse | |
| dl = simpleHTTP . getRequest | |
| safeDL :: Int -> String -> IO (Either Error HttpResponse) | |
| safeDL n = liftST n dl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment