2

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))
blaze
  • 4,326
  • 18
  • 23

1 Answers1

0

As far as I know, happstack uses monad-control, and you should be able to use its lifted catch in the appropriate place (as long as you also force the exception appropriately as well): http://hackage.haskell.org/package/monad-control-0.3.1

sclv
  • 38,665
  • 7
  • 99
  • 204