handleIServFailure :: IServ -> String -> SomeException -> IO a
handleIServFailure IServ{..} op e = do
  ex <- getProcessExitCode iservProcess
  case ex of
    Just (ExitFailure n) -> do
      errorContents <- maybe (return "") hGetContents iservErrors
      res <- evaluate (force errorContents)
      throw (InstallationError ("While in operation " ++ op
                             ++ ":\nException: " ++ show e
                             ++ "\neta-serv terminated (" ++ show n ++ ")\n" ++ res))
    _ | Just (MessageParseFailure msg left off) <- fromException e -> do
        errorContents <- maybe (return "") hGetContents iservErrors
        res <- evaluate (force errorContents)
        throw (InstallationError ("While in operation " ++ op
                              ++ ":\nFailed to parse: " ++ msg
                              ++ "\nRemaining: " ++ left
                              ++ "\nOffset: " ++ show off ++ "\n" ++ res))
      | otherwise -> do
        {- TODO: When debugging JVM exit code 143's
           putStrLn $ "TERMINATING PROCESS:\nSomeException: " ++ show e
                   ++ "\nProcess Exit Code: " ++ show ex -}
        terminateProcess iservProcess
        _ <- waitForProcess iservProcess
        throw e

-- -----------------------------------------------------------------------------
-- Starting and stopping the iserv process
handleIServFailure :: IServ -> String -> SomeException -> IO a
handleIServFailure IServ{..} op e = do
  ex <- getProcessExitCode iservProcess
  case ex of
    Just (ExitFailure n) -> do
      errorContents <- maybe (return "") hGetContents iservErrors
      res <- evaluate (force errorContents)
      throw (InstallationError ("While in operation " ++ op
                             ++ ":\nException: " ++ show e
                             ++ "\neta-serv terminated (" ++ show n ++ ")\n" ++ res))
    _ | Just (MessageParseFailure msg left off) <- fromException e -> do
        errorContents <- maybe (return "") hGetContents iservErrors
        res <- evaluate (force errorContents)
        throw (InstallationError ("While in operation " ++ op
                              ++ ":\nFailed to parse: " ++ msg
                              ++ "\nRemaining: " ++ left
                              ++ "\nOffset: " ++ show off ++ "\n" ++ res))
      | otherwise -> do
        {- TODO: When debugging JVM exit code 143's
           putStrLn $ "TERMINATING PROCESS:\nSomeException: " ++ show e
                   ++ "\nProcess Exit Code: " ++ show ex -}
        terminateProcess iservProcess
        _ <- waitForProcess iservProcess
        throw e

-- -----------------------------------------------------------------------------
-- Starting and stopping the iserv process
handleIServFailure IServ{..} op e = do
  ex <- getProcessExitCode iservProcess
  case ex of
    Just (ExitFailure n) -> do
      errorContents <- maybe (return "") hGetContents iservErrors
      res <- evaluate (force errorContents)
      throw (InstallationError ("While in operation " ++ op
                             ++ ":\nException: " ++ show e
                             ++ "\neta-serv terminated (" ++ show n ++ ")\n" ++ res))
    _ | Just (MessageParseFailure msg left off) <- fromException e -> do
        errorContents <- maybe (return "") hGetContents iservErrors
        res <- evaluate (force errorContents)
        throw (InstallationError ("While in operation " ++ op
                              ++ ":\nFailed to parse: " ++ msg
                              ++ "\nRemaining: " ++ left
                              ++ "\nOffset: " ++ show off ++ "\n" ++ res))
      | otherwise -> do
        {- TODO: When debugging JVM exit code 143's
           putStrLn $ "TERMINATING PROCESS:\nSomeException: " ++ show e
                   ++ "\nProcess Exit Code: " ++ show ex -}
        terminateProcess iservProcess
        _ <- waitForProcess iservProcess
        throw e

-- -----------------------------------------------------------------------------
-- Starting and stopping the iserv process