[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to know where ERB stopped?

matt

3/20/2009 7:15:00 PM

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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...
2 Answers

Matthias Reitinger

3/21/2009 6:09:00 PM

0

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]):

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].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

[1]: http://www.pastie....
--
Posted via http://www.ruby-....

matt

3/21/2009 9:09:00 PM

0

Matthias Reitinger <reitinge@in.tum.de> wrote:

> 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]):
>
> 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].to_i
> puts "While evaluating line #{line} of the template:"
> 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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...