module Happstack.Server.Error where
import Control.Monad.Trans.Except (ExceptT, runExceptT)
import Happstack.Server.Monads (ServerPartT)
import Happstack.Server.Internal.Monads (WebT, UnWebT, withRequest, mkWebT, runServerPartT, ununWebT)
import Happstack.Server.Response (ok, toResponse)
import Happstack.Server.Types (Request, Response)
spUnwrapErrorT:: Monad m => (e -> ServerPartT m a)
-> Request
-> UnWebT (ExceptT e m) a
-> UnWebT m a
spUnwrapErrorT :: forall (m :: * -> *) e a.
Monad m =>
(e -> ServerPartT m a)
-> Request -> UnWebT (ExceptT e m) a -> UnWebT m a
spUnwrapErrorT e -> ServerPartT m a
handler Request
rq = \UnWebT (ExceptT e m) a
x -> do
Either e (Maybe (Either Response a, FilterFun Response))
err <- forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT UnWebT (ExceptT e m) a
x
case Either e (Maybe (Either Response a, FilterFun Response))
err of
Left e
e -> forall (m :: * -> *) a. WebT m a -> UnWebT m a
ununWebT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. ServerPartT m a -> Request -> WebT m a
runServerPartT (e -> ServerPartT m a
handler e
e) Request
rq
Right Maybe (Either Response a, FilterFun Response)
a -> forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Either Response a, FilterFun Response)
a
simpleErrorHandler :: (Monad m) => String -> ServerPartT m Response
simpleErrorHandler :: forall (m :: * -> *). Monad m => String -> ServerPartT m Response
simpleErrorHandler String
err = forall (m :: * -> *) a. FilterMonad Response m => a -> m a
ok forall a b. (a -> b) -> a -> b
$ forall a. ToMessage a => a -> Response
toResponse forall a b. (a -> b) -> a -> b
$ (String
"An error occured: " forall a. [a] -> [a] -> [a]
++ String
err)
errorHandlerSP :: (Monad m) => (Request -> e -> WebT m a) -> ServerPartT (ExceptT e m) a -> ServerPartT m a
errorHandlerSP :: forall (m :: * -> *) e a.
Monad m =>
(Request -> e -> WebT m a)
-> ServerPartT (ExceptT e m) a -> ServerPartT m a
errorHandlerSP Request -> e -> WebT m a
handler ServerPartT (ExceptT e m) a
sps = forall (m :: * -> *) a. (Request -> WebT m a) -> ServerPartT m a
withRequest forall a b. (a -> b) -> a -> b
$ \Request
req -> forall (m :: * -> *) a. UnWebT m a -> WebT m a
mkWebT forall a b. (a -> b) -> a -> b
$ do
Either e (Maybe (Either Response a, FilterFun Response))
eer <- forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. WebT m a -> UnWebT m a
ununWebT forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. ServerPartT m a -> Request -> WebT m a
runServerPartT ServerPartT (ExceptT e m) a
sps Request
req
case Either e (Maybe (Either Response a, FilterFun Response))
eer of
Left e
err -> forall (m :: * -> *) a. WebT m a -> UnWebT m a
ununWebT (Request -> e -> WebT m a
handler Request
req e
err)
Right Maybe (Either Response a, FilterFun Response)
res -> forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Either Response a, FilterFun Response)
res
{-# DEPRECATED errorHandlerSP "Use spUnwrapErrorT" #-}