Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit b30d664ea6f47595fbd01f5c0637d735b08f77f0 1 parent 8c8cfed
@rkh rkh authored
Showing with 7 additions and 6 deletions.
  1. +7 −6 lib/sinatra/base.rb
View
13 lib/sinatra/base.rb
@@ -465,6 +465,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)
@@ -477,11 +478,8 @@ 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
@@ -489,6 +487,7 @@ def render(engine, data, options={}, locals={}, &block)
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?
@@ -500,12 +499,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 b30d664

Please sign in to comment.
Something went wrong with that request. Please try again.