Skip to content

Instantly share code, notes, and snippets.

@jb55
Created July 26, 2011 13:58
Show Gist options
  • Select an option

  • Save jb55/1106824 to your computer and use it in GitHub Desktop.

Select an option

Save jb55/1106824 to your computer and use it in GitHub Desktop.
Handy error handling combinators
data Error = Timeout
| Exception SomeException
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
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