You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Without this change, Happstack's `failResponse` is not always sent.
For instance:
main :: IO ()
main = do
let sendResponse = ok $ toResponse (fromJust Nothing :: String)
simpleHTTP nullConf sendResponse
This server tries to send a string, but can't because the string's
value is bottom. Before this change, the server replies with status
200 and empty content, while outputting "HTTP request failed with:
Maybe.fromJust: Nothing" to stderr. This is because the response is
only evaluated into WHNF inside the exception handler in Handler.hs,
and the exception is caught by the surrounding catch block in
Listen.hs. After this change, the server replies with status 500 and
the `failResponse` page.
It's possible that this slows down the Happstack server. Some parts
of the Response object may be evaluated but not needed. Strictly
evaluating the response at a finer level might be necessary.
Additionally, with NFData Response, clients can call `deepseq`
themselves on the response object in order to catch any bottom values
that it contains, log them and display a custom error page:
import Control.DeepSeq (deepseq)
import Control.Monad.Catch (SomeException, handle)
handleServerPartError :: ServerPart Response -> ServerPart Response
handleServerPartError s = handle errorPage $ do
res <- s
deepseq res (return res)
where
errorPage :: SomeException -> ServerPart Response
errorPage _ = (internalServerError $ toResponse "Custom error page!")
main :: IO ()
main = do
let sendResponse = ok $ toResponse (fromJust Nothing :: String)
simpleHTTP nullConf (handleServerPartError sendResponse)
Let me know if you want to merge in this patch. I'll be using it in
my own applications for sure, unless it proves to cause any
problems. :)
0 commit comments