From 4ed2187377bb915c0eac7f93f20afa3d16f79a5d Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 9 Apr 2015 12:49:13 +0200 Subject: [PATCH] Use cached result if there is a network error --- src/libexpr/download.cc | 2 +- src/libexpr/download.hh | 2 ++ src/libexpr/primops.cc | 19 ++++++++++++------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/libexpr/download.cc b/src/libexpr/download.cc index 050e8d69c..b23e31967 100644 --- a/src/libexpr/download.cc +++ b/src/libexpr/download.cc @@ -100,7 +100,7 @@ struct Curl CURLcode res = curl_easy_perform(curl); if (res == CURLE_WRITE_ERROR && etag == expectedETag) return false; if (res != CURLE_OK) - throw Error(format("unable to download ‘%1%’: %2% (%3%)") + throw DownloadError(format("unable to download ‘%1%’: %2% (%3%)") % url % curl_easy_strerror(res) % res); long httpStatus = 0; diff --git a/src/libexpr/download.hh b/src/libexpr/download.hh index aa4fd5083..65396e5de 100644 --- a/src/libexpr/download.hh +++ b/src/libexpr/download.hh @@ -13,4 +13,6 @@ struct DownloadResult DownloadResult downloadFile(string url, string expectedETag = ""); +MakeError(DownloadError, Error) + } diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 33ec26d26..8823efe82 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1551,20 +1551,25 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v, if (!skip) { - if (expectedETag.empty()) + if (storePath.empty()) printMsg(lvlInfo, format("downloading ‘%1%’...") % url); else printMsg(lvlInfo, format("checking ‘%1%’...") % url); - auto res = downloadFile(url, expectedETag); + try { + auto res = downloadFile(url, expectedETag); - if (!res.cached) - storePath = store->addTextToStore(name, res.data, PathSet(), state.repair); + if (!res.cached) + storePath = store->addTextToStore(name, res.data, PathSet(), state.repair); - assert(!storePath.empty()); - replaceSymlink(storePath, fileLink); + assert(!storePath.empty()); + replaceSymlink(storePath, fileLink); - writeFile(dataFile, url + "\n" + res.etag + "\n" + int2String(time(0)) + "\n"); + writeFile(dataFile, url + "\n" + res.etag + "\n" + int2String(time(0)) + "\n"); + } catch (DownloadError & e) { + if (storePath.empty()) throw; + printMsg(lvlError, format("warning: %1%; using cached result") % e.msg()); + } } if (unpack) {