Skip to content

Commit

Permalink
Better error when helper does not exist (see issue #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-psarga committed Jun 26, 2019
1 parent 96174f5 commit 4967cbb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/ruby-handlebars/tree.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module Handlebars
class UnknownHelper < StandardError
end

module Tree
class TreeItem < Struct
def eval(context)
Expand Down Expand Up @@ -50,7 +53,9 @@ def _eval(context)

class Helper < TreeItem.new(:name, :parameters, :block)
def _eval(context)
context.get_helper(name.to_s).apply(context, parameters, block)
helper = context.get_helper(name.to_s)
raise(UnknownHelper, "Helper \"#{name}\" does not exist" )if helper.nil?
helper.apply(context, parameters, block)
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/handlebars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,16 @@ def self.escape(value)
end
end
end

context 'regression tests' do
context 'when an unknown helper is called in a template' do
it 'should provide a useful error message with inline helpers' do
expect{ evaluate('{{unknown "This will hardly work" }}') }.to raise_exception(Handlebars::UnknownHelper, 'Helper "unknown" does not exist')
end

it 'should provide a useful error message with block helpers' do
expect{ evaluate('{{#unknown}}This will hardly work{{/unknown}}') }.to raise_exception(Handlebars::UnknownHelper, 'Helper "unknown" does not exist')
end
end
end
end

0 comments on commit 4967cbb

Please sign in to comment.