Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
rkh committed Dec 14, 2010
1 parent e537977 commit 9d5a718
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/sinatra/base.rb
Expand Up @@ -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)

Expand All @@ -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?
Expand All @@ -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)
Expand Down

0 comments on commit 9d5a718

Please sign in to comment.