-- | Some useful functions if you want to wrap the 'ServerPartT' monad transformer around the 'ErrorT' monad transformer. e.g., @'ServerPartT' ('ErrorT' e m) a@. This allows you to use 'throwError' and 'catchError' inside your monad.  
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)

--------------------------------------------------------------
-- Error Handling
--------------------------------------------------------------

-- | Flatten @'ServerPartT' ('ErrorT' e m) a@ into a @'ServerPartT' m
-- a@ so that it can be use with 'simpleHTTP'.  Used with
-- 'mapServerPartT'', e.g.,
--
-- > simpleHTTP conf $ mapServerPartT' (spUnWrapErrorT simpleErrorHandler)  $ myPart `catchError` errorPart
--
-- Note that in this example, @simpleErrorHandler@ will only be run if @errorPart@ throws an error. You can replace @simpleErrorHandler@ with your own custom error handler.
--
-- see also: 'simpleErrorHandler'
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

-- | A simple error handler which can be used with 'spUnwrapErrorT'.
-- 
-- It returns the error message as a plain text message to the
-- browser. More sophisticated behaviour can be achieved by calling
-- your own custom error handler instead.
--
-- see also: 'spUnwrapErrorT'
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)

-- | This 'ServerPart' modifier enables the use of 'throwError' and
-- 'catchError' inside the 'WebT' actions, by adding the 'ErrorT'
-- monad transformer to the stack.
--
-- You can wrap the complete second argument to 'simpleHTTP' in this
-- function.
--
-- DEPRECATED: use 'spUnwrapErrorT' instead.
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" #-}