Why can't inline template be rescued?

69 views
Skip to first unread message

kopf1988

unread,
Oct 18, 2015, 5:17:24 PM10/18/15
to Ruby on Rails: Talk
I am attempting to debug some complicated inline templates, because they are generated dynamically with the complicated way this program works.

However when doing render inline: "Template here" rescue false, an error is still thrown if there's an error in the Template.

I tried this in IRB and it's very weird!
ActionView::Template.new("Test <% if Crash %>", "inline template", handler, :locals => {}).render("Test2",{}) rescue false

This returns an error, even though we would expect the Rescue to rescue us from the bad code in the inline template. This worked in an old version of Rails.

Any ideas?

Breaking out into multiple lines also didn't help
begin
ActionView::Template.new("Test <% if Crash %>", "inline template", handler, :locals => {}).render("Test2",{})
rescue
false
end

Still has an error.

=Ryan

Andy Ogzewalla

unread,
Oct 19, 2015, 5:04:14 AM10/19/15
to Ruby on Rails: Talk
By default rescue only matches StandardError. If you are getting another type of Exception then your usage of rescue will not be sufficient to handle it. You will need to use the multiline form of begin/rescue.

```
begin
  ActionView::Template.new("Test <% if Crash %>", "inline template", handler, :locals => {}).render("Test2",{})
rescue Exception  # You should replace Exception with a more specific class or classes if possible.
  false
end

kopf1988

unread,
Nov 21, 2015, 11:33:42 AM11/21/15
to Ruby on Rails: Talk
You are fantastic. This previously worked in 1.9, so I'm assuming something changed so that SyntaxError is no longer under StandardError. Problem solved, thank you for your help.

=Ryan
Reply all
Reply to author
Forward
0 new messages