From a8f054657448348ef8564d06958269fd4cf1adb9 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sun, 24 Aug 2008 19:12:44 +0000 Subject: Move CGIError stuff to FastCGIUtils. --- src/server/FastCGIUtils.hs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'src/server/FastCGIUtils.hs') diff --git a/src/server/FastCGIUtils.hs b/src/server/FastCGIUtils.hs index 762127f7c..2cf64af91 100644 --- a/src/server/FastCGIUtils.hs +++ b/src/server/FastCGIUtils.hs @@ -1,9 +1,12 @@ +{-# LANGUAGE DeriveDataTypeable #-} module FastCGIUtils (initFastCGI, loopFastCGI, - DataRef, newDataRef, getData) where + DataRef, newDataRef, getData, + throwCGIError, handleCGIErrors) where import Control.Concurrent import Control.Exception import Control.Monad +import Data.Dynamic import Data.IORef import Prelude hiding (catch) import System.Directory @@ -14,6 +17,7 @@ import System.IO.Unsafe import System.Posix import System.Time + import Network.FastCGI initFastCGI :: IO () @@ -119,4 +123,19 @@ getData loadData ref file = liftIO $ -- Logging logError :: String -> IO () -logError s = hPutStrLn stderr s \ No newline at end of file +logError s = hPutStrLn stderr s + +-- * General CGI Error exception mechanism + +data CGIError = CGIError { cgiErrorCode :: Int, cgiErrorMessage :: String, cgiErrorText :: [String] } + deriving Typeable + +throwCGIError :: Int -> String -> [String] -> CGI a +throwCGIError c m t = throwCGI $ DynException $ toDyn $ CGIError c m t + +handleCGIErrors :: CGI CGIResult -> CGI CGIResult +handleCGIErrors x = x `catchCGI` \e -> case e of + DynException d -> case fromDynamic d of + Nothing -> throw e + Just (CGIError c m t) -> outputError c m t + _ -> throw e -- cgit v1.2.3