A platform for community discussion. Free, open, simple.
FIX: Rescue and warn when error is encountered in `DiscourseIpInfo.mmdb_download` (#28134)
Alan Guo Xiang Tan3193afe7ca8f
diff --git a/lib/discourse_ip_info.rb b/lib/discourse_ip_info.rbindex e7e28fb0d8a..ab697db919b 100644--- a/lib/discourse_ip_info.rb+++ b/lib/discourse_ip_info.rb@@ -55,31 +55,27 @@ class DiscourseIpInfoendend- begin- gz_file =- FileHelper.download(- url,- max_file_size: 100.megabytes,- tmp_file_name: "#{name}.gz",- validate_uri: false,- follow_redirect: true,- extra_headers:,- )-- filename = File.basename(gz_file.path)-- dir = "#{Dir.tmpdir}/#{SecureRandom.hex}"-- Discourse::Utils.execute_command("mkdir", "-p", dir)-- Discourse::Utils.execute_command("cp", gz_file.path, "#{dir}/#{filename}")-- Discourse::Utils.execute_command("tar", "-xzvf", "#{dir}/#{filename}", chdir: dir)-- Dir["#{dir}/**/*.mmdb"].each { |f| FileUtils.mv(f, mmdb_path(name)) }- rescue => e- Discourse.warn_exception(e, message: "MaxMind database download failed.")- end+ gz_file =+ FileHelper.download(+ url,+ max_file_size: 100.megabytes,+ tmp_file_name: "#{name}.gz",+ validate_uri: false,+ follow_redirect: true,+ extra_headers:,+ )++ filename = File.basename(gz_file.path)++ dir = "#{Dir.tmpdir}/#{SecureRandom.hex}"++ Discourse::Utils.execute_command("mkdir", "-p", dir)+ Discourse::Utils.execute_command("cp", gz_file.path, "#{dir}/#{filename}")+ Discourse::Utils.execute_command("tar", "-xzvf", "#{dir}/#{filename}", chdir: dir)++ Dir["#{dir}/**/*.mmdb"].each { |f| FileUtils.mv(f, mmdb_path(name)) }+ rescue => e+ Discourse.warn_exception(e, message: "MaxMind database #{name} download failed.")ensureFileUtils.rm_r(dir, force: true) if dirgz_file&.close!diff --git a/spec/lib/discourse_ip_info_spec.rb b/spec/lib/discourse_ip_info_spec.rbindex dd65bb4b920..3525d9a0ff4 100644--- a/spec/lib/discourse_ip_info_spec.rb+++ b/spec/lib/discourse_ip_info_spec.rb@@ -60,5 +60,28 @@ RSpec.describe DiscourseIpInfo dodescribed_class.mmdb_download("GeoLite2-City")end++ it "should not throw an error and instead log the exception when database file fails to download" do+ original_logger = Rails.logger+ Rails.logger = fake_logger = FakeLogger.new++ global_setting :maxmind_license_key, "license_key"+ global_setting :maxmind_account_id, "account_id"++ stub_request(+ :get,+ "https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz",+ ).with(basic_auth: %w[account_id license_key]).to_return(status: 500, body: nil, headers: {})++ expect do described_class.mmdb_download("GeoLite2-City") end.not_to raise_error++ expect(fake_logger.warnings.length).to eq(1)++ expect(fake_logger.warnings.first).to include(+ "MaxMind database GeoLite2-City download failed. 500 Error",+ )+ ensure+ Rails.logger = original_logger+ endendend