# How to know where ERB stopped?

**URL:** <https://www.ruby-forum.com/t/how-to-know-where-erb-stopped/163481>\
**Category:** Ruby\
**Created:** [March 20, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/how-to-know-where-erb-stopped/163481 "2009-03-20T20:20:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 20, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/how-to-know-where-erb-stopped/163481/1 "2009-03-20T20:20:48Z")

</div>

Let’s say I’ve got an ERB template:

text1  
\<%= thing1() %\>  
text2  
\<%= thing2() %\>  
text3  
\<%= thing3() %\>  
text4

But let’s say we get an error (exception) during the execution of  
thing2(). How can I find out that this is where the error occurred? I  
don’t want to instrument thing1, thing2, thing3, etc; I’m hoping I can  
just find out what ERB was doing when we errored out. Thx - m.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 21, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/how-to-know-where-erb-stopped/163481/2 "2009-03-21T19:12:20Z")

</div>

matt neuburg wrote:

> Let’s say I’ve got an ERB template:
> 
> text1  
> \<%= thing1() %\>  
> text2  
> \<%= thing2() %\>  
> text3  
> \<%= thing3() %\>  
> text4
> 
> But let’s say we get an error (exception) during the execution of  
> thing2(). How can I find out that this is where the error occurred? I  
> don’t want to instrument thing1, thing2, thing3, etc; I’m hoping I can  
> just find out what ERB was doing when we errored out. Thx - m.

The following code illustrates how this could be accomplished (also  
available as a pastie[1](http://www.pastie.org/422933)):

require ‘erb’

def thing1(); “foo” end  
def thing2(); raise Exception.new(“Something went wrong”) end  
def thing3(); “bar” end

template = \<\<ERB  
text1  
\<%= thing1() %\>  
text2  
\<%= thing2() %\>  
text3  
\<%= thing3() %\>  
text4  
ERB

erb = ERB.new(template)

begin  
erb.run  
rescue Exception =\> e  
puts “Exception: #{e}”  
line = e.backtrace.grep(/^(erb)/)[0].split(‘:’)[1](http://www.pastie.org/422933).to\_i  
puts “While evaluating line #{line} of the template:”  
puts template.split(“\n”)[line-1]  
puts "Backtrace: ", e.backtrace  
end

Hope that helps!

-Matthias

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 21, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/how-to-know-where-erb-stopped/163481/3 "2009-03-21T22:16:02Z")

</div>

Matthias R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> def thing1(); “foo” end  
> text4  
> puts template.split(“\n”)[line-1]  
> puts "Backtrace: ", e.backtrace  
> end
> 
> Hope that helps!

Very much so, thank you. Grepping and parsing the backtrace is the exact  
clue that I needed. m.
