I'm kind of a noob at Haskell, so I'm not entirely sure if this is a Happstack question or a general Haskell question.
Here's an example of the difficulty I'm having. This code "theoretically" renders some content, but actually throws an error:
throwsError :: String
throwsError = fromJust Nothing
-- no error page
main :: IO ()
main = do
simpleHTTP nullConf $ do
decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
ok $ toResponse throwsError
This error does not crash the whole program. Fortunately, Happstack catches any errors thrown while handling the request, like a web server should. However, unfortunately it does not display any kind of error page to the user. It responds with status code 200 and empty content.
Now, if I simply output the error string first:
-- yes error page
main :: IO ()
main = do
simpleHTTP nullConf $ do
decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
lift $ putStrLn throwsError -- added this line
ok $ toResponse throwsError
Happstack returns status 500 and displays an error page. Why is Happstack behaving like this?
The ServerPart
monad implements MonadThrow
, so I tried importing Control.Monad.Catch (handle)
and writing this, but it didn't do what I expected; it again returned 200 with no content:
showErrorPage :: SomeException -> ServerPart Response
showErrorPage _ = internalServerError $ toResponse "Error"
-- also no error page
main :: IO ()
main = do
simpleHTTP nullConf $ handle showErrorPage $ do
decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096)
ok $ toResponse throwsError
In case it isn't clear, I would like to handle all errors thrown, so I can log them and display a custom error page. (Except, of course, for errors that are thrown while logging and displaying the error page). Guidance would be appreciated.