From 485a87f22f96498727deab044fc74931164a1acd Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 2 Apr 2020 19:09:17 +0200 Subject: [PATCH] Don't barf on registry parse errors --- src/libfetchers/registry.cc | 73 ++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/src/libfetchers/registry.cc b/src/libfetchers/registry.cc index e452e8497..77e5da741 100644 --- a/src/libfetchers/registry.cc +++ b/src/libfetchers/registry.cc @@ -17,47 +17,52 @@ std::shared_ptr Registry::read( if (!pathExists(path)) return std::make_shared(type); - auto json = nlohmann::json::parse(readFile(path)); + try { - auto version = json.value("version", 0); + auto json = nlohmann::json::parse(readFile(path)); - // FIXME: remove soon - if (version == 1) { - auto flakes = json["flakes"]; - for (auto i = flakes.begin(); i != flakes.end(); ++i) { - auto url = i->value("url", i->value("uri", "")); - if (url.empty()) - throw Error("flake registry '%s' lacks a 'url' attribute for entry '%s'", - path, i.key()); - registry->entries.push_back( - {inputFromURL(i.key()), inputFromURL(url), {}}); - } - } + auto version = json.value("version", 0); - else if (version == 2) { - for (auto & i : json["flakes"]) { - auto toAttrs = jsonToAttrs(i["to"]); - Attrs extraAttrs; - auto j = toAttrs.find("dir"); - if (j != toAttrs.end()) { - extraAttrs.insert(*j); - toAttrs.erase(j); + // FIXME: remove soon + if (version == 1) { + auto flakes = json["flakes"]; + for (auto i = flakes.begin(); i != flakes.end(); ++i) { + auto url = i->value("url", i->value("uri", "")); + if (url.empty()) + throw Error("flake registry '%s' lacks a 'url' attribute for entry '%s'", + path, i.key()); + registry->entries.push_back( + {inputFromURL(i.key()), inputFromURL(url), {}}); } - auto exact = i.find("exact"); - registry->entries.push_back( - Entry { - .from = inputFromAttrs(jsonToAttrs(i["from"])), - .to = inputFromAttrs(toAttrs), - .extraAttrs = extraAttrs, - .exact = exact != i.end() && exact.value() - }); } + + else if (version == 2) { + for (auto & i : json["flakes"]) { + auto toAttrs = jsonToAttrs(i["to"]); + Attrs extraAttrs; + auto j = toAttrs.find("dir"); + if (j != toAttrs.end()) { + extraAttrs.insert(*j); + toAttrs.erase(j); + } + auto exact = i.find("exact"); + registry->entries.push_back( + Entry { + .from = inputFromAttrs(jsonToAttrs(i["from"])), + .to = inputFromAttrs(toAttrs), + .extraAttrs = extraAttrs, + .exact = exact != i.end() && exact.value() + }); + } + } + + else + throw Error("flake registry '%s' has unsupported version %d", path, version); + + } catch (nlohmann::json::exception & e) { + warn("cannot parse flake registry '%s': %s", path, e.what()); } - else - throw Error("flake registry '%s' has unsupported version %d", path, version); - - return registry; }