Question

I got an exception (used Prelude.head on an empty list) that made all the http-request after that return a 502/505 exception (and the happstack app prints "HTTP request failed with: send resource vanished (Broken pipe)" in stdout).

My question is this: What is the best practice for controlling exceptions in Happstack? Should I use something else than simpleHTTP or simply use Control.Exception.catch on the controller function?

It currently looks similar to the example in the Crash Course:

main :: IO ()
main = do
  hSetEncoding stdout utf8
  bracket (startSystemState (Proxy :: Proxy AppState)) createCheckpointAndShutdown $ 
         \_control ->
           simpleHTTP nullConf { port = 1729 } $ do
             _ <- liftIO today
             decodeBody policy
             controller
    where
      createCheckpointAndShutdown control = do 
          createCheckpoint control
          shutdownSystem control
Was it helpful?

Solution

What do you mean by, "the server went down"?

If you are handling a Request and do head [], that should only kill the thread that is handling that particular request. The server itself should continue running and processing other requests.

If you have found a way to kill the whole server, that is a bug and bug report / test case would be highly appreciated.

If only the current thread is being killed, and you want to catch that exception, then I think you need to use MonadPeelIO,

http://hackage.haskell.org/packages/archive/monad-peel/0.1/doc/html/Control-Monad-IO-Peel.html

Someone has submitted a patch for that, but it has not been reviewed/merged yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top