Skip to content

Commit 313a40d

Browse files
committed
Add specs, better error handling
1 parent 977b131 commit 313a40d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/react_on_rails/locales/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def generate_translations
121121
val = flatten(translation[key])
122122
translations = translations.deep_merge(key => val)
123123
defaults = defaults.deep_merge(flatten_defaults(val)) if key == default_locale
124+
rescue Psych::Exception => e
125+
raise ReactOnRails::Error, "Error parsing #{f}: #{e.message}"
124126
end
125127
[translations.to_json, defaults.to_json]
126128
end

spec/react_on_rails/locales_to_js_spec.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,37 @@ module ReactOnRails
9898
end
9999

100100
describe "with symbols in yaml" do
101+
let(:locale_dir) { File.expand_path("fixtures/i18n/locales_symbols", __dir__) }
102+
101103
before do
102104
ReactOnRails.configure do |config|
103-
config.i18n_yml_dir = File.expand_path("fixtures/i18n/locales_symbols", __dir__)
104-
config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
105+
config.i18n_dir = i18n_dir
106+
config.i18n_yml_dir = locale_dir
105107
end
106108
end
107109

108110
after do
109111
ReactOnRails.configure do |config|
112+
config.i18n_dir = nil
110113
config.i18n_yml_dir = nil
111114
config.i18n_yml_safe_load_options = nil
112115
end
113116
end
114117

115-
it "handles locale loading" do
118+
it "handles unsafe locale loading" do
119+
ReactOnRails.configure do |config|
120+
config.i18n_yml_safe_load_options = { permitted_classes: [Symbol] }
121+
end
122+
116123
expect { described_class.new }.not_to raise_error
117124
end
125+
126+
it "raises error with filename when not permitted" do
127+
expect { described_class.new }.to raise_error(
128+
ReactOnRails::Error,
129+
"Error parsing #{locale_dir}/de.yml: Tried to load unspecified class: Symbol"
130+
)
131+
end
118132
end
119133
end
120134
end

0 commit comments

Comments
 (0)