From 30d25dd856cf539ca0dc798310180007abee7b6b Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Sun, 25 Mar 2012 16:26:02 +0200 Subject: [PATCH] Switch to new-style exception handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, we're using Prelude.catch to handle I/O errors in htools. This style of error handling has been deprecated for a while, but it still used to work without warnings. However, the GHC release 7.4 has started to emit deprecation warnings for it, so we change to the Control.Exception module; the code is a bit less clean since we only care about I/O errors (but Control.Exception deals with other error types too), so we have to filter the exceptions. Note that the new style exception handling is not really "new"; it has existed since at least GHC 6.12, which is our oldest supported compiler. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- htools/Ganeti/HTools/ExtLoader.hs | 4 +++- htools/Ganeti/HTools/Rapi.hs | 5 ++++- htools/htools.hs | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/htools/Ganeti/HTools/ExtLoader.hs b/htools/Ganeti/HTools/ExtLoader.hs index 2defadecf..bd258f5f3 100644 --- a/htools/Ganeti/HTools/ExtLoader.hs +++ b/htools/Ganeti/HTools/ExtLoader.hs @@ -34,7 +34,9 @@ module Ganeti.HTools.ExtLoader ) where import Control.Monad +import Control.Exception import Data.Maybe (isJust, fromJust) +import Prelude hiding (catch) import System.FilePath import System.IO import Text.Printf (hPrintf) @@ -53,7 +55,7 @@ import Ganeti.HTools.Utils (sepSplit, tryRead, exitIfBad, exitWhen) -- | Error beautifier. wrapIO :: IO (Result a) -> IO (Result a) -wrapIO = flip catch (return . Bad . show) +wrapIO = flip catch (\e -> return . Bad . show $ (e::IOException)) -- | Parses a user-supplied utilisation string. parseUtilisation :: String -> Result (String, DynUtil) diff --git a/htools/Ganeti/HTools/Rapi.hs b/htools/Ganeti/HTools/Rapi.hs index d52abab1c..710bfbb6f 100644 --- a/htools/Ganeti/HTools/Rapi.hs +++ b/htools/Ganeti/HTools/Rapi.hs @@ -30,6 +30,7 @@ module Ganeti.HTools.Rapi , parseData ) where +import Control.Exception import Data.List (isPrefixOf) import Data.Maybe (fromMaybe) #ifndef NO_CURL @@ -37,6 +38,7 @@ import Network.Curl import Network.Curl.Types () #endif import Control.Monad +import Prelude hiding (catch) import Text.JSON (JSObject, fromJSObject, decodeStrict) import Text.JSON.Types (JSValue(..)) import Text.Printf (printf) @@ -83,7 +85,8 @@ getUrl url = do -- | Helper to convert I/O errors in 'Bad' values. ioErrToResult :: IO a -> IO (Result a) ioErrToResult ioaction = - catch (ioaction >>= return . Ok) (return . Bad . show) + catch (ioaction >>= return . Ok) + (\e -> return . Bad . show $ (e::IOException)) -- | Append the default port if not passed in. formatHost :: String -> String diff --git a/htools/htools.hs b/htools/htools.hs index f35c18cdf..2e847d7f2 100644 --- a/htools/htools.hs +++ b/htools/htools.hs @@ -25,10 +25,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA module Main (main) where +import Control.Exception +import Control.Monad (guard) import Data.Char (toLower) +import Prelude hiding (catch) import System.Environment import System.Exit import System.IO +import System.IO.Error (isDoesNotExistError) import Ganeti.HTools.Utils import Ganeti.HTools.CLI (parseOpts) @@ -47,7 +51,8 @@ usage name = do main :: IO () main = do - binary <- getEnv "HTOOLS" `catch` const getProgName + binary <- catchJust (guard . isDoesNotExistError) + (getEnv "HTOOLS") (const getProgName) let name = map toLower binary boolnames = map (\(x, y) -> (x == name, Just y)) personalities case select Nothing boolnames of -- GitLab