use Either rather than throwing an exception

This commit is contained in:
Mark Wotton 2016-03-21 13:39:49 -04:00
parent 62961ef931
commit 29c335af56
2 changed files with 7 additions and 10 deletions

View File

@ -700,18 +700,18 @@ get url = request $ do
--
-- > followRedirect
followRedirect :: Yesod site
=> YesodExample site ()
=> YesodExample site (Either T.Text ())
followRedirect = do
mr <- getResponse
case mr of
Nothing -> failure "no response, so no redirect to follow"
Nothing -> return $ Left "no response, so no redirect to follow"
Just r -> do
if not ((H.statusCode $ simpleStatus r) `elem` [301,303])
then failure "followRedirect called, but previous request was not a redirect"
then return $ Left "followRedirect called, but previous request was not a redirect"
else do
case lookup "Location" (simpleHeaders r) of
Nothing -> failure "No location header set"
Just h -> get (TE.decodeUtf8 h)
Nothing -> return $ Left "No location header set"
Just h -> get (TE.decodeUtf8 h) >> return (Right ())
-- | Sets the HTTP method used by the request.
--

View File

@ -231,13 +231,10 @@ main = hspec $ do
bodyContains "we have been successfully redirected"
yit "throws an exception when no redirect was returned" $ do
yit "returns a Left when no redirect was returned" $ do
get ("/" :: Text)
statusIs 200
-- This appears to be an HUnitFailure, which is not
-- exported, so I'm catching SomeException instead.
(r :: Either SomeException ()) <- try followRedirect
statusIs 200
r <- followRedirect
liftIO $ assertBool "expected exception" $ isLeft r
instance RenderMessage LiteApp FormMessage where