Skip to content
This repository

Include the missing key in "missing interpolation argument" errors. #166

Merged
merged 1 commit into from over 1 year ago

3 participants

Henrik Nyh Sven Fuchs Christopher Dell
Henrik Nyh

It's a bit annoying to manually compare the passed-in hash to the arguments, so we fixed it. Hope you'll merge.

Some things I noticed:

test/i18n/exceptions_test.rb had a commented-out test about including the missing key in an exception accessor. Couldn't figure out from git blame why it was commented out in the first place ("cleanup"), so I restored it.

backend/interpolation_compiler.rb did this:

        def missing_key(key)
          "raise(MissingInterpolationArgument.new(#{key}, self))"
        end

That looks like forgotten, possibly dead code. Before my pull request, the actual method profile was values, string; the key wasn't passed. I just modified it to pass {} for values, so it's about as broken as it was, but that class should probably be reviewed.

Sven Fuchs
Owner

thanks @henrik!

looks like a good idea to me, but i'll leave this up to @tigrish :)

Christopher Dell tigrish merged commit 09c0aa7 into from September 16, 2012
Christopher Dell tigrish closed this September 16, 2012
Christopher Dell
Collaborator

Good stuff, it seems so obvious!
Of course we want to see which key is missing :)

Henrik Nyh

:) Great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 16, 2012
Henrik Nyh Include the key in "missing interpolation argument" errors. efd9f1c
This page is out of date. Refresh to see the latest.
2  lib/i18n/backend/interpolation_compiler.rb
@@ -77,7 +77,7 @@ def nil_key(key)
77 77
         end
78 78
 
79 79
         def missing_key(key)
80  
-          "raise(MissingInterpolationArgument.new(#{key}, self))"
  80
+          "raise(MissingInterpolationArgument.new(#{key}, {}, self))"
81 81
         end
82 82
 
83 83
         def reserved_key(key)
8  lib/i18n/exceptions.rb
@@ -81,10 +81,10 @@ def initialize(entry, count)
81 81
   end
82 82
 
83 83
   class MissingInterpolationArgument < ArgumentError
84  
-    attr_reader :values, :string
85  
-    def initialize(values, string)
86  
-      @values, @string = values, string
87  
-      super "missing interpolation argument in #{string.inspect} (#{values.inspect} given)"
  84
+    attr_reader :key, :values, :string
  85
+    def initialize(key, values, string)
  86
+      @key, @values, @string = key, values, string
  87
+      super "missing interpolation argument #{key.inspect} in #{string.inspect} (#{values.inspect} given)"
88 88
     end
89 89
   end
90 90
 
2  lib/i18n/interpolate/ruby.rb
@@ -21,7 +21,7 @@ def interpolate_hash(string, values)
21 21
           '%'
22 22
         else
23 23
           key = ($1 || $2).to_sym
24  
-          value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(values, string))
  24
+          value = values.key?(key) ? values[key] : raise(MissingInterpolationArgument.new(key, values, string))
25 25
           value = value.call(values) if value.respond_to?(:call)
26 26
           $3 ? sprintf("%#{$3}", value) : value
27 27
         end
4  test/i18n/exceptions_test.rb
@@ -54,14 +54,14 @@ def test_invalid_locale_stores_locale
54 54
   test "MissingInterpolationArgument stores key and string" do
55 55
     assert_raise(I18n::MissingInterpolationArgument) { force_missing_interpolation_argument }
56 56
     force_missing_interpolation_argument do |exception|
57  
-      # assert_equal :bar, exception.key
  57
+      assert_equal :bar, exception.key
58 58
       assert_equal "%{bar}", exception.string
59 59
     end
60 60
   end
61 61
 
62 62
   test "MissingInterpolationArgument message contains the missing and given arguments" do
63 63
     force_missing_interpolation_argument do |exception|
64  
-      assert_equal 'missing interpolation argument in "%{bar}" ({:baz=>"baz"} given)', exception.message
  64
+      assert_equal 'missing interpolation argument :bar in "%{bar}" ({:baz=>"baz"} given)', exception.message
65 65
     end
66 66
   end
67 67
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.