Permalink
Browse files

Don't rescue Errno::ENOENT in render

All Errno::ENOENT exceptions raised during layout (not template) rendering
where caught assuming this would indicate that there is no layout file.
However, if the Errno::ENOENT was raised inside the layout (say, from a
missing partial), that exceptions was rescued, too.

With this patch, it will not only not raise an error if the template is
missing, it will just raise an exception if the layout is missing and the
layout option hasn't been set explicitely. That way, unexpected behavior can
more easily be traced back to missing templates.

Fixes #135.
  • Loading branch information...
1 parent e537977 commit 9d5a718b05a51b1ea090af55a859f05bf51507c3 @rkh rkh committed Dec 8, 2010
Showing with 7 additions and 6 deletions.
  1. +7 −6 lib/sinatra/base.rb
View
@@ -461,6 +461,7 @@ def render(engine, data, options={}, locals={}, &block)
views = options.delete(:views) || settings.views || "./views"
@default_layout = :layout if @default_layout.nil?
layout = options.delete(:layout)
+ eat_errors = layout.nil?
layout = @default_layout if layout.nil? or layout == true
content_type = options.delete(:content_type) || options.delete(:default_content_type)
@@ -473,18 +474,16 @@ def render(engine, data, options={}, locals={}, &block)
# render layout
if layout
- begin
- options = options.merge(:views => views, :layout => false)
- output = render(engine, layout, options, locals) { output }
- rescue Errno::ENOENT
- end
+ options = options.merge(:views => views, :layout => false, :eat_errors => eat_errors)
+ catch(:layout_missing) { output = render(engine, layout, options, locals) { output }}
end
output.extend(ContentTyped).content_type = content_type if content_type
output
end
def compile_template(engine, data, options, views)
+ eat_errors = options.delete :eat_errors
template_cache.fetch engine, data, options do
template = Tilt[engine]
raise "Template engine not found: #{engine}" if template.nil?
@@ -496,12 +495,14 @@ def compile_template(engine, data, options, views)
body = body.call if body.respond_to?(:call)
template.new(path, line.to_i, options) { body }
else
+ found = false
path = ::File.join(views, "#{data}.#{engine}")
Tilt.mappings.each do |ext, klass|
- break if File.exists?(path)
+ break if found = File.exists?(path)
next unless klass == template
path = ::File.join(views, "#{data}.#{ext}")
end
+ throw :layout_missing if eat_errors and !found
template.new(path, 1, options)
end
when data.is_a?(Proc) || data.is_a?(String)

0 comments on commit 9d5a718

Please sign in to comment.