From a14e8888272a331bb6cdb1abadb03d4f5f22ec1d Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Wed, 5 Feb 2014 11:03:58 -0500 Subject: Handle errors better in Weather plugin --- src/Plugins/Monitors/Weather.hs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/Plugins/Monitors') diff --git a/src/Plugins/Monitors/Weather.hs b/src/Plugins/Monitors/Weather.hs index 4030352..f2cc131 100644 --- a/src/Plugins/Monitors/Weather.hs +++ b/src/Plugins/Monitors/Weather.hs @@ -133,8 +133,7 @@ stationUrl station = defUrl ++ station ++ ".TXT" getData :: String -> IO String getData station = do let request = getRequest (stationUrl station) - result <- simpleHTTP request - catch (getResponseBody result) errHandler + catch (simpleHTTP request >>= getResponseBody) errHandler where errHandler :: IOException -> IO String errHandler _ = return "" @@ -155,13 +154,16 @@ weatherReady :: [String] -> Monitor Bool weatherReady str = do let station = head str request = headRequest (stationUrl station) - result <- io $ simpleHTTP request - case result of - Left _ -> return False - Right response -> do - case rspCode response of - -- Permission or network errors are failures; anything else is - -- recoverable. - (4, _, _) -> return False - (5, _, _) -> return False - (_, _, _) -> return True + io $ catch (simpleHTTP request >>= checkResult) errHandler + where errHandler :: IOException -> IO Bool + errHandler _ = return False + checkResult result = do + case result of + Left _ -> return False + Right response -> do + case rspCode response of + -- Permission or network errors are failures; anything + -- else is recoverable. + (4, _, _) -> return False + (5, _, _) -> return False + (_, _, _) -> return True -- cgit v1.2.3