From 4967cbbd763ec0ceec5d144e70d3b3164b758a7b Mon Sep 17 00:00:00 2001 From: Vincent Pretre Date: Wed, 26 Jun 2019 17:24:57 +0200 Subject: [PATCH] Better error when helper does not exist (see issue #4) --- lib/ruby-handlebars/tree.rb | 7 ++++++- spec/handlebars_spec.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/ruby-handlebars/tree.rb b/lib/ruby-handlebars/tree.rb index 68ebe79..5fb15c5 100644 --- a/lib/ruby-handlebars/tree.rb +++ b/lib/ruby-handlebars/tree.rb @@ -1,4 +1,7 @@ module Handlebars + class UnknownHelper < StandardError + end + module Tree class TreeItem < Struct def eval(context) @@ -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 diff --git a/spec/handlebars_spec.rb b/spec/handlebars_spec.rb index c7fc970..6d35b6c 100644 --- a/spec/handlebars_spec.rb +++ b/spec/handlebars_spec.rb @@ -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