FIX: do not raise exception when svg path is nil (#13844)

FIX: do not raise exception when svg path is nil (#13844)

Bug was introduced here: https://github.com/discourse/discourse/commit/f7ab852e123afe22b9a594bd43af712b7cebd98b

If path is nil, it should not raise an exception and continue logging the error.

diff --git a/app/models/theme_field.rb b/app/models/theme_field.rb
index 8477456..603e1f0 100644
--- a/app/models/theme_field.rb
+++ b/app/models/theme_field.rb
@@ -180,10 +180,10 @@ class ThemeField < ActiveRecord::Base
       path = Discourse.store.path_for(upload)
     end
 
-    content = File.read(path)
     error = nil
 
     begin
+      content = File.read(path)
       svg_file = Nokogiri::XML(content) do |config|
         config.options = Nokogiri::XML::ParseOptions::NOBLANKS
       end
diff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rb
index 9854ce3..5ebc77d 100644
--- a/spec/models/theme_field_spec.rb
+++ b/spec/models/theme_field_spec.rb
@@ -430,6 +430,11 @@ HTML
       theme_field.destroy!
       expect(SvgSprite.custom_svg_sprites(theme.id).size).to eq(0)
     end
+
+    it 'crashes gracefully when svg is invalid' do
+      FileStore::LocalStore.any_instance.stubs(:path_for).returns(nil)
+      expect(theme_field.validate_svg_sprite_xml).to match("Error with icons-sprite")
+    end
   end
 
 end

GitHub sha: 88aa0136e372b232b89e1298d87e67ad9a4cb27f

This commit appears in #13844 which was approved by martin. It was merged by SamSaffron.