salvia-0.0.4: src/Network/Salvia/Handlers/Fallback.hs
module Network.Salvia.Handlers.Fallback (hOr, hEither) where
import Control.Applicative
import Control.Monad.State
import Network.Salvia.Httpd
import Network.Protocol.Http
{- |
Until I figure out how to do this correctly, this handler somewow implements
the MonadPlus/Alternative instance using a custom function. When the first
handler fails the response will be reset and the second handler is executed.
-}
hOr :: Handler a -> Handler a -> Handler a
hOr h0 h1 = do
a <- h0
st <- gets (status . response)
if statusFailure st
then reset >> h1
else return a
hEither :: Handler a -> Handler b -> Handler (Either a b)
hEither h0 h1 = (Left <$> h0) `hOr` (Right <$> h1)