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