X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=Lucu.git;a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FAbortion.hs;h=064a97ebd8671232b9053a6382d93c85927df318;hp=ff6915762b41b3795f948274766de52b6686fda6;hb=51eda5b;hpb=1000bdc46cfe7b3ae550ff24ccea9f440f11b42a diff --git a/Network/HTTP/Lucu/Abortion.hs b/Network/HTTP/Lucu/Abortion.hs index ff69157..064a97e 100644 --- a/Network/HTTP/Lucu/Abortion.hs +++ b/Network/HTTP/Lucu/Abortion.hs @@ -1,61 +1,50 @@ +{-# LANGUAGE + UnicodeSyntax + #-} +-- |Aborting the computation of 'Network.HTTP.Lucu.Resource.Resource' +-- in any 'Prelude.IO' monads or arrows. module Network.HTTP.Lucu.Abortion - ( Abortion(..) - , abort -- MonadIO m => StatusCode -> [ (String, String) ] -> String -> m a - , abortSTM -- StatusCode -> [ (String, String) ] -> String -> STM a - , abortA -- ArrowIO a => StatusCode -> [ (String, String) ] -> String -> a b c - , aboPage -- Config -> Abortion -> String + ( Abortion + , mkAbortion + , mkAbortion' + + , abort ) where - -import Control.Arrow -import Control.Arrow.ArrowIO -import Control.Concurrent.STM -import Control.Exception -import Control.Monad.Trans -import GHC.Conc (unsafeIOToSTM) -import Data.Dynamic -import Network.HTTP.Lucu.Config -import Network.HTTP.Lucu.DefaultPage -import Network.HTTP.Lucu.Headers -import Network.HTTP.Lucu.Response -import System.IO.Unsafe -import Text.XML.HXT.Arrow.WriteDocument -import Text.XML.HXT.Arrow.XmlArrow -import Text.XML.HXT.Arrow.XmlIOStateArrow -import Text.XML.HXT.DOM.XmlKeywords - - -data Abortion = Abortion { - aboStatus :: StatusCode - , aboHeaders :: Headers - , aboMessage :: String - } deriving (Show, Typeable) - - -abort :: MonadIO m => StatusCode -> [ (String, String) ] -> String -> m a -abort status headers msg - = let abo = Abortion status headers msg - exc = DynException (toDyn abo) - in - liftIO $ throwIO exc - - -abortSTM :: StatusCode -> [ (String, String) ] -> String -> STM a -abortSTM status headers msg - = unsafeIOToSTM $ abort status headers msg - - -abortA :: ArrowIO a => StatusCode -> [ (String, String) ] -> String -> a b c -abortA status headers msg - = arrIO0 $ abort status headers msg - - -aboPage :: Config -> Abortion -> String -aboPage conf abo - = let [html] = unsafePerformIO - $ runX ( mkDefaultPage conf (aboStatus abo) (txt $ aboMessage abo) - >>> - writeDocumentToString [(a_indent, v_1)] - ) - in - html +import Control.Exception +import Control.Monad.Trans +import Data.Ascii (Ascii, CIAscii) +import Data.Monoid.Unicode +import Data.Text (Text) +import Network.HTTP.Lucu.Abortion.Internal +import Network.HTTP.Lucu.Headers +import Network.HTTP.Lucu.Response +import Prelude.Unicode + +-- |Construct an 'Abortion' with additional headers and an optional +-- message text. +mkAbortion ∷ StatusCode sc ⇒ sc → [(CIAscii, Ascii)] → Maybe Text → Abortion +{-# INLINE mkAbortion #-} +mkAbortion sc hdr msg + = Abortion { + aboStatus = fromStatusCode sc + , aboHeaders = toHeaders hdr + , aboMessage = msg + } + +-- |Construct an 'Abortion' without any additional headers but with a +-- message text. +mkAbortion' ∷ StatusCode sc ⇒ sc → Text → Abortion +{-# INLINE mkAbortion' #-} +mkAbortion' sc msg + = Abortion { + aboStatus = fromStatusCode sc + , aboHeaders = (∅) + , aboMessage = Just msg + } + +-- |Throw an 'Abortion' in a 'MonadIO', including the very +-- 'Network.HTTP.Lucu.Resource.Resource' monad. +abort ∷ MonadIO m ⇒ Abortion → m a +{-# INLINE abort #-} +abort = liftIO ∘ throwIO