Skip to content

Commit

Permalink
Catch exceptions whilst trying to fetch metadata (#325)
Browse files Browse the repository at this point in the history
The previous way was only handling the case where the metadata fetched
could not be correctly parsed. Now it will also disable the global
cache if the HTTP request fails for any reason.
  • Loading branch information
carlosdagos authored and f-f committed Jul 19, 2019
1 parent 3f5aa37 commit 70d5d5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
27 changes: 16 additions & 11 deletions src/Spago/GlobalCache.hs
Expand Up @@ -100,16 +100,21 @@ getMetadata cacheFlag = do
Nothing -> mempty
Just a -> a

downloadMeta = do
metaBS <- (Http.httpBS metaURL >>= pure . Http.getResponseBody)
case decodeStrict' metaBS of
Nothing -> do
downloadMeta = handleAny
(\err -> do
echoDebug $ "Metadata fetch failed with exception: " <> tshow err
echo "WARNING: Unable to download GitHub metadata, global cache will be disabled"
pure mempty
Just meta -> do
assertDirectory globalCacheDir
liftIO $ BS.writeFile globalPathToMeta metaBS
pure meta
pure mempty)
(do
metaBS <- Http.getResponseBody `fmap` Http.httpBS metaURL
case decodeStrict' metaBS of
Nothing -> do
echo "WARNING: Unable to parse GitHub metadata, global cache will be disabled"
pure mempty
Just meta -> do
assertDirectory globalCacheDir
liftIO $ BS.writeFile globalPathToMeta metaBS
pure meta)

case cacheFlag of
-- If we need to skip the cache we just get an empty map
Expand All @@ -123,7 +128,7 @@ getMetadata cacheFlag = do
echo "Searching for packages cache metadata.."

-- Check if the metadata is in global cache and fresher than 1 day
shouldDownloadMeta <- try (liftIO $ do
shouldDownloadMeta <- tryIO (liftIO $ do
fileExists <- testfile $ Turtle.decodeString globalPathToMeta
lastModified <- getModificationTime globalPathToMeta
now <- Time.getCurrentTime
Expand All @@ -132,7 +137,7 @@ getMetadata cacheFlag = do
pure $ not (fileExists && fileIsRecentEnough)
) >>= \case
Right v -> pure v
Left (err :: IOException) -> do
Left err -> do
echoDebug $ "Unable to read metadata file. Error was: " <> tshow err
pure True

Expand Down
3 changes: 2 additions & 1 deletion src/Spago/Prelude.hs
Expand Up @@ -47,6 +47,7 @@ module Spago.Prelude
, pathSeparator
, headMay
, for
, handleAny
, try
, tryIO
, makeAbsolute
Expand Down Expand Up @@ -106,7 +107,7 @@ import Turtle (ExitCode (..), FilePath, appendo
systemStrictWithErr, testdir, testfile)
import UnliftIO (MonadUnliftIO, withRunInIO)
import UnliftIO.Directory (getModificationTime, makeAbsolute)
import UnliftIO.Exception (IOException, try, tryIO)
import UnliftIO.Exception (IOException, handleAny, try, tryIO)
import UnliftIO.Process (callCommand)

-- | Generic Error that we throw on program exit.
Expand Down

0 comments on commit 70d5d5c

Please sign in to comment.