Permalink
Browse files

Fix #180 - make exception message more helpful on YAML syntax error

  • Loading branch information...
knapo committed Dec 21, 2012
1 parent a1f6552 commit 4de4ed4df4a78849ac46d363575e8637d1b7d04f
Showing with 18 additions and 8 deletions.
  1. +5 −5 lib/i18n/backend/base.rb
  2. +3 −3 lib/i18n/exceptions.rb
  3. +7 −0 test/i18n/load_path_test.rb
  4. +3 −0 test/test_data/locales/invalid/syntax.yml
View
@@ -159,7 +159,9 @@ def load_file(filename)
type = File.extname(filename).tr('.', '').downcase
raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
data = send(:"load_#{type}", filename)
- raise InvalidLocaleData.new(filename) unless data.is_a?(Hash)
+ unless data.is_a?(Hash)
+ raise InvalidLocaleData.new(filename, 'expects it to return a hash, but does not')
+ end
data.each { |locale, d| store_translations(locale, d || {}) }
end
@@ -174,10 +176,8 @@ def load_rb(filename)
def load_yml(filename)
begin
YAML.load_file(filename)
- rescue TypeError
- nil
- rescue SyntaxError
- nil
+ rescue TypeError, SyntaxError => e
+ raise InvalidLocaleData.new(filename, e.inspect)
end
end
end
View
@@ -29,9 +29,9 @@ def initialize(locale)
class InvalidLocaleData < ArgumentError
attr_reader :filename
- def initialize(filename)
- @filename = filename
- super "can not load translations from #{filename}, expected it to return a hash, but does not"
+ def initialize(filename, exception_message)
+ @filename, @exception_message = filename, exception_message
+ super "can not load translations from #{filename}: #{exception_message}"
end
end
@@ -19,6 +19,13 @@ def setup
end
end
+ test "loading an invalid yml file raises an InvalidLocaleData exception" do
+ assert_raise I18n::InvalidLocaleData do
+ I18n.load_path = [[locales_dir + '/invalid/syntax.yml']]
+ I18n.t(:'foo.bar', :default => "baz")
+ end
+ end
+
test "adding arrays of filenames to the load path does not break locale loading" do
I18n.load_path << Dir[locales_dir + '/*.{rb,yml}']
assert_equal "baz", I18n.t(:'foo.bar')
@@ -0,0 +1,3 @@
+foo
+bar: baz
+ foo

0 comments on commit 4de4ed4

Please sign in to comment.