@@ -1,10 +1,14 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
13module Network.Wai.Handler.Warp.Settings where
24
35import Control.Exception
46import qualified Data.ByteString as S
7+import qualified Data.ByteString.Lazy.Char8 as L8
58import Data.Conduit
69import Data.Conduit.Network (HostPreference (HostIPv4))
710import GHC.IO.Exception (IOErrorType(..))
11+import qualified Network.HTTP.Types as H
812import Network.Wai
913import Network.Wai.Handler.Warp.Timeout
1014import Network.Wai.Handler.Warp.Types
@@ -21,6 +25,12 @@ data Settings = Settings
2125 { settingsPort :: Int -- ^ Port to listen on. Default value: 3000
2226 , settingsHost :: HostPreference -- ^ Default value: HostIPv4
2327 , settingsOnException :: Maybe Request -> SomeException -> IO () -- ^ What to do with exceptions thrown by either the application or server. Default: ignore server-generated exceptions (see 'InvalidRequest') and print application-generated applications to stderr.
28+ , settingsOnExceptionResponse :: SomeException -> Response
29+ -- ^ A function to create `Response` when an exception occurs.
30+ --
31+ -- Default: 500, text/plain, \"Something went wrong\"
32+ --
33+ -- Since 2.0.3
2434 , settingsOnOpen :: IO () -- ^ What to do when a connection is open. Default: do nothing.
2535 , settingsOnClose :: IO () -- ^ What to do when a connection is close. Default: do nothing.
2636 , settingsTimeout :: Int -- ^ Timeout value in seconds. Default value: 30
@@ -52,6 +62,7 @@ defaultSettings = Settings
5262 { settingsPort = 3000
5363 , settingsHost = HostIPv4
5464 , settingsOnException = defaultExceptionHandler
65+ , settingsOnExceptionResponse = defaultExceptionResponse
5566 , settingsOnOpen = return ()
5667 , settingsOnClose = return ()
5768 , settingsTimeout = 30
@@ -83,3 +94,10 @@ defaultExceptionHandler _ e = throwIO e `catches` handlers
8394
8495 sh :: SomeException -> IO ()
8596 sh x = hPrint stderr x
97+
98+defaultExceptionResponse :: SomeException -> Response
99+defaultExceptionResponse _ = responseLBS H.internalServerError500 [(H.hContentType, "text/plain")] "Something went wrong"
100+
101+-- | Default implementation of 'settingsOnExceptionResponse' for the debugging purpose. 500, text/plain, a showed exception.
102+exceptionResponseForDebug :: SomeException -> Response
103+exceptionResponseForDebug e = responseLBS H.internalServerError500 [(H.hContentType, "text/plain")] (L8.pack $ "Exception: " ++ show e)