Skip to content

Commit

Permalink
migrate Sqs example to errors 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidb committed Oct 31, 2015
1 parent 218aaae commit 24f56ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
16 changes: 8 additions & 8 deletions Examples/Sqs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ main = do
let receiveMessageReq = Sqs.ReceiveMessage Nothing [] (Just 1) [] sqsQName (Just 20)
let numMessages = length messages
removedMsgs <- replicateM numMessages $ do
msgs <- eitherT (const $ return []) return . retryT 2 $ do
msgs <- exceptT (const $ return []) return . retryT 2 $ do
Sqs.ReceiveMessageResponse r <- liftIO $ Aws.simpleAws cfg sqscfg receiveMessageReq
case r of
[] -> left "no message received"
_ -> right r
[] -> throwE "no message received"
_ -> return r
putStrLn $ "number of messages received: " ++ show (length msgs)
forM msgs (\msg -> do
-- here we remove a message, delete it from the queue, and then return the
Expand All @@ -88,24 +88,24 @@ main = do
{- | Let's make sure the queue was actually deleted and that the same number of queues exist at when
| the program ends as when it started.
-}
eitherT T.putStrLn T.putStrLn . retryT 4 $ do
exceptT T.putStrLn T.putStrLn . retryT 4 $ do
qUrls <- liftIO $ do
putStrLn $ "Listing all queueus to check to see if " ++ show (Sqs.qName sqsQName) ++ " is gone"
Sqs.ListQueuesResponse qUrls_ <- Aws.simpleAws cfg sqscfg $ Sqs.ListQueues Nothing
mapM_ T.putStrLn qUrls_
return qUrls_

if qUrl `elem` qUrls
then left $ " *\n *\n * Warning, '" <> sshow qName <> "' was not deleted\n"
then throwE $ " *\n *\n * Warning, '" <> sshow qName <> "' was not deleted\n"
<> " * This is probably just a race condition."
else right $ " The queue '" <> sshow qName <> "' was correctly deleted"
else return $ " The queue '" <> sshow qName <> "' was correctly deleted"

retryT :: MonadIO m => Int -> EitherT T.Text m a -> EitherT T.Text m a
retryT :: MonadIO m => Int -> ExceptT T.Text m a -> ExceptT T.Text m a
retryT i f = go 1
where
go x
| x >= i = fmapLT (\e -> "error after " <> sshow x <> " retries: " <> e) f
| otherwise = f `catchT` \_ -> do
| otherwise = f `catchE` \_ -> do
liftIO $ threadDelay (1000000 * min 60 (2^(x-1)))
go (succ x)

Expand Down
35 changes: 18 additions & 17 deletions aws.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Library
unordered-containers >= 0.2,
utf8-string >= 0.3 && < 1.1,
vector >= 0.10,
xml-conduit >= 1.2 && <1.4
xml-conduit >= 1.2 && <1.4,
errors

if !impl(ghc >= 7.6)
Build-depends: ghc-prim
Expand Down Expand Up @@ -296,22 +297,22 @@ Executable DynamoDb
Default-Language: Haskell2010


-- Executable Sqs
-- Main-is: Sqs.hs
-- Hs-source-dirs: Examples
--
-- if !flag(Examples)
-- Buildable: False
-- else
-- Buildable: True
-- Build-depends:
-- base == 4.*,
-- aws,
-- errors >= 1.4.7 && < 2.0,
-- text >=0.11,
-- transformers >= 0.3
--
-- Default-Language: Haskell2010
Executable Sqs
Main-is: Sqs.hs
Hs-source-dirs: Examples

if !flag(Examples)
Buildable: False
else
Buildable: True
Build-depends:
base == 4.*,
aws,
errors >= 2.0,
text >=0.11,
transformers >= 0.3

Default-Language: Haskell2010

-- test-suite sqs-tests
-- type: exitcode-stdio-1.0
Expand Down

0 comments on commit 24f56ba

Please sign in to comment.