From c2e453fbb9b4ea8858d07d165abfd37f35309db0 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 16 Mar 2020 15:30:22 +0200 Subject: [PATCH] vpm: make 'v install' more resilient to server failures --- cmd/tools/vpm.v | 67 +++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/cmd/tools/vpm.v b/cmd/tools/vpm.v index 134dcf9ea1..df4af46e25 100644 --- a/cmd/tools/vpm.v +++ b/cmd/tools/vpm.v @@ -129,36 +129,12 @@ fn vpm_install(module_names []string) { exit(2) } mut errors := 0 - url := get_working_server_url() for n in module_names { name := n.trim_space() - modurl := url + '/jsmod/$name' - r := http.get(modurl) or { - println('Http server did not respond to our request for ${modurl}.') - println('Error details: $err') + mod := get_module_meta_info(name) or { errors++ - continue - } - if r.status_code == 404 { - println('Skipping module "$name", since $url reported that "$name" does not exist.') - errors++ - continue - } - if r.status_code != 200 { - println('Skipping module "$name", since $url responded with $r.status_code http status code. Please try again later.') - errors++ - continue - } - s := r.text - mod := json.decode(Mod,s) or { - errors++ - println('Skipping module "$name", since its information is not in json format.') - continue - } - if ('' == mod.url || '' == mod.name) { - errors++ - // a possible 404 error, which means a missing module? - println('Skipping module "$name", since it is missing name or url information.') + println('Errors while retrieving meta data for module ${name}:') + println(err) continue } mut vcs := mod.vcs @@ -464,3 +440,40 @@ fn verbose_println(s string) { println(s) } } + +fn get_module_meta_info(name string) ?Mod { + mut errors := []string + for server_url in default_vpm_server_urls { + modurl := server_url + '/jsmod/$name' + verbose_println('Retrieving module metadata from: $modurl ...') + r := http.get(modurl) or { + errors << 'Http server did not respond to our request for ${modurl}.' + errors << 'Error details: $err' + continue + } + if r.status_code == 404 { + errors << 'Skipping module "$name", since $server_url reported that "$name" does not exist.' + continue + } + if r.status_code != 200 { + errors << 'Skipping module "$name", since $server_url responded with $r.status_code http status code. Please try again later.' + continue + } + s := r.text + if s.len > 0 && s[0] != `{` { + errors << 'Invalid json data' + errors << s.trim_space().limit(100) + '...' + continue + } + mod := json.decode(Mod,s) or { + errors << 'Skipping module "$name", since its information is not in json format.' + continue + } + if ('' == mod.url || '' == mod.name) { + errors << 'Skipping module "$name", since it is missing name or url information.' + continue + } + return mod + } + return error(errors.join_lines()) +}