From c7a831a28007b540e6551499ef37522face8f3e7 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Tue, 17 Mar 2020 23:21:38 +0100 Subject: [PATCH] Improve error handling in download When download fails, delete the partial file, so it doesn't corrupt the cache. --- lib/GHCup/Download.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/GHCup/Download.hs b/lib/GHCup/Download.hs index bbf2428..b798625 100644 --- a/lib/GHCup/Download.hs +++ b/lib/GHCup/Download.hs @@ -135,10 +135,9 @@ getDownloads urlSource = do L.ByteString smartDl uri' = do let path = view pathL' uri' - cacheDir <- liftIO $ ghcupCacheDir + cacheDir <- liftIO $ ghcupCacheDir json_file <- (cacheDir ) <$> urlBaseName path - e <- - liftIO $ doesFileExist json_file + e <- liftIO $ doesFileExist json_file if e then do accessTime <- @@ -300,8 +299,14 @@ download dli dest mfn -- download fd <- liftIO $ createRegularFileFd newFilePerms destFile let stepper = fdWrite fd - flip finally (liftIO $ closeFd fd) - $ reThrowAll DownloadFailed + flip onException + (liftIO $ hideError doesNotExistErrorType $ deleteFile destFile) + $ flip finally (liftIO $ closeFd fd) + $ catchAllE + (\e -> + (liftIO $ hideError doesNotExistErrorType $ deleteFile destFile) + >> (throwE . DownloadFailed $ e) + ) $ downloadInternal True https host fullPath port stepper liftE $ checkDigest dli destFile