I'm writing web application using happstack-lite. When my ServerPart Response handler calls pure function and this function calls error, entire handler fails and happstack prints default error message.
handler :: ServerPart Response
handler = do
head [] `seq` ok $ toResponse $ H.html "mymessage"
Server error: Prelude.head: empty list
I wonder if it is possible to catch this exception and return custom message. Unfortunately, I can't figure out how to use catch in ServerPart monad.
Ideally, my handler should look like code below:
handler :: ServerPart Response
handler = do
catch
(head [] `seq` ok $ toResponse $ H.html "my message")
(ok $ toResponse $ H.html "my error")
Update: saving my solution for the history
handler :: ServerPart Response
handler = do
let good = ok $ toResponse $ H.html "my message"
let bad = ok $ toResponse $ H.html "my error"
join (liftIO $ catch
(do
let n = head (tail [1])
_ <- print n
return good
)
(\(E.ErrorCall _) -> return bad))