How to print FULL stacktrace of exception w/ line #?


#1

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don’t mind it is verbose.

begin

Math.sqrt (-1)
rescue => e
puts e.inspect
end

e only prints out a message.

Am I missing anything real simple?

Thanks
~Andrew C.


#2

Quoth Andrew C.:

end

e only prints out a message.

Am I missing anything real simple?

Thanks
~Andrew C.

Don’t rescue it, full stack trace! :stuck_out_tongue:


#3

Konrad M. wrote:

Don’t rescue it, full stack trace! :stuck_out_tongue:

In the situation, I want to rescue, and run some other logic, since I
don’t want the whole class to die.


#4

On 9/19/07, Andrew C. removed_email_address@domain.invalid wrote:

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don’t mind it is verbose.

begin

Math.sqrt (-1)
rescue => e
puts e.inspect
puts e.backtrace
ensure
andrew.brain << open(“ri”){|ri| ri.read(‘Exception’)}
end


#5

You can check utilrb’s #full_message at
http://www.laas.fr/~sjoyeux/darcs/utilrb/lib/utilrb/exception/full_message.rb

(utilrb is also available as a gem on rubyforge :))


#6

I miss Java.

I run into this kind of short sighted decision far too often with Ruby.
Ugh.


#7

Sylvain J. wrote:

Don’t rescue it, full stack trace! :stuck_out_tongue:
… or not
Ruby removes in-betweeen lines if a backtrace is ‘too long’. The limit is
not configurable, so it you have long backtraces (and if you need what is
in the middle) you’re fucked.

You can get that information when you rescue the exception -
Exception#backtrace is an array of caller information.


#8

You can get that information when you rescue the exception -
Exception#backtrace is an array of caller information.
Yes, so I had to rescue all exceptions in all threads I spawn to have a
proper backtrace (even more so since with abort_on_exception the
exception
is not shown /at all/).

Sometimes it drived me crazy …

Sylvain J.


#9

Sylvain J. wrote:

You can get that information when you rescue the exception -
Exception#backtrace is an array of caller information.
Yes, so I had to rescue all exceptions in all threads I spawn to have a
proper backtrace (even more so since with abort_on_exception the
exception
is not shown /at all/).

Sometimes it drived me crazy …

Sylvain J.

You might be able to rewrite the Thread.new method and have it catch and
display exceptions (and raise them).
GL!
-Roger


#10

Don’t rescue it, full stack trace! :stuck_out_tongue:
… or not
Ruby removes in-betweeen lines if a backtrace is ‘too long’. The limit
is
not configurable, so it you have long backtraces (and if you need what
is
in the middle) you’re fucked.


#11

e.backtrace works fine for me. In my case, I don’t need traces in the
middle.

Thanks for all the help!


#12

Michael F. wrote in post #555437:

On 9/19/07, Andrew C. removed_email_address@domain.invalid wrote:

The ruby interpreter prints out a full trace of the exception.

However, all the expection I got is an one line message. I need to know
where the exception happened in my code. I don’t mind it is verbose.

begin

Math.sqrt (-1)
rescue => e
puts e.inspect
puts e.backtrace
ensure
andrew.brain << open(“ri”){|ri| ri.read(‘Exception’)}
end

hahah :slight_smile: nice one


#13

Friends i want to write backtrace inside teardown method but i am
unable to get the syntax. Here is the script that i have

def teardown
if @check==1
$driver.close
puts “passed if condition”
else
puts “passed else condtion”
$driver.save_screenshot(‘IM.png’)
sleep 3
#------ i want to put backtrace here—
$driver.switch_to.default_content
sleep 1
puts “Switched back from Iframe”

$driver.find_element(:name, “headerForm:logoutLink”).click
puts “User successfully logged out”
sleep 2

$driver.close
puts “test case passed”

  assert_equal [], @verification_errors
  $driver.save_screenshot('IM.png')
puts "test case failed"

end


#14

On 12/05/2013 11:23 PM, saurav panda wrote:

sleep 3
puts “test case passed”

   assert_equal [], @verification_errors
   $driver.save_screenshot('IM.png')
 puts "test case failed"

end

“caller” will return the stack trace as an array. For example:

 puts caller

will print out the stack trace with file names, line numbers, and method
names.

If you need to rescue an exception, you at least need a “rescue”
clause[1].

For example:

def teardown

rescue => e
puts e.backtrace
end

-Justin

[1]https://github.com/ruby/ruby/blob/trunk/doc/syntax/exceptions.rdoc


#15

Justin C. wrote in post #1129827:

On 12/05/2013 11:23 PM, saurav panda wrote:

sleep 3
puts “test case passed”

   assert_equal [], @verification_errors
   $driver.save_screenshot('IM.png')
 puts "test case failed"

end

“caller” will return the stack trace as an array. For example:

 puts caller

will print out the stack trace with file names, line numbers, and method
names.

If you need to rescue an exception, you at least need a “rescue”
clause[1].

For example:

def teardown

rescue => e
puts e.backtrace
end

-Justin

[1]https://github.com/ruby/ruby/blob/trunk/doc/syntax/exceptions.rdoc

justin
i have used this
def teardown

rescue => e
puts e.backtrace
end

It is showing exception as un defined method backtrace

can we use rescue inside teardown and that too without “begin”

only puts caller is enough to get the failed element, or anything extra
needed


#16

On 12/06/2013 12:06 AM, saurav panda wrote:

For example:

It is showing exception as un defined method backtrace

Please check your code or post your exact code and error message.

$ irb
2.0.0p247 :001 > def teardown
2.0.0p247 :002?> raise
2.0.0p247 :003?> rescue => e
2.0.0p247 :004?> puts e.backtrace
2.0.0p247 :005?> end
=> nil
2.0.0p247 :006 > teardown
(irb):2:in teardown' (irb):6:inirb_binding’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86:in
eval' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/workspace.rb:86:inevaluate’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/context.rb:380:in
evaluate' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:492:inblock (2 levels) in eval_input’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:624:in
signal_status' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:489:inblock in eval_input’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:247:in
block (2 levels) in each_top_level_statement' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:233:inloop’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:233:in
block in each_top_level_statement' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:232:incatch’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb/ruby-lex.rb:232:in
each_top_level_statement' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:488:ineval_input’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:397:in
block in start' /home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:396:incatch’
/home/justin/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/irb.rb:396:in
start' /home/justin/.rvm/rubies/ruby-2.0.0-p247/bin/irb:12:in
=> nil

-Justin


#17

On 12/06/2013 12:43 AM, saurav panda wrote:

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

You left out the important part, which is how you are adding rescue and
what the error is when you try to do so.

-Justin


#18

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

require “rubygems”
require “selenium-webdriver”
gem “test-unit”
require “test/unit”

class Search_Export < Test::Unit::TestCase

def setup
$driver = Selenium::WebDriver.for :ie
$base_url = “https://qa-pec.razorsight.com/sso2
$accept_next_alert = true
$driver.manage.timeouts.implicit_wait = 10
$verification_errors = []

#-----------------User Login-----------------
puts “User Log in”
$driver.get($base_url + “/login.jsf”)
$driver.find_element(:id, “loginForm:username”).clear
$driver.find_element(:id, “loginForm:username”).send_keys
“removed_email_address@domain.invalid”
$driver.find_element(:id, “loginForm:password”).clear
$driver.find_element(:id, “loginForm:password”).send_keys “Saurav12345”
$driver.find_element(:id, “loginForm:loginBtn”).click

puts “User has successfully logged in”
end

def test_Search_Export
assert_equal “Razorsight - Profit Enhancement Center”, $driver.title
puts “Assertion for IMLink passed”

puts “Wait for 5sec for page to load”
sleep 5

$driver.switch_to.frame $driver.find_element(:id, “baseFrame”)
puts “Switched to Iframe”

puts “Wait for 5sec for page load to complete”
sleep 5
puts “User Logged in”

#------------------------User Logged in------------------------
#-------------------Searching an Invoice-----------------------
@loop = 1
while @loop <= 2 do
$driver.find_element(:id, “j_id16:searchControlTab_lbl”).click
$driver.find_element(:id, “j_id761:invoiceSearchForm:acntnp”).clear

#-- this is error element ->j_id761:invoiceSearchForm:acntnp —

$driver.find_element(:id, “j_id761:invoiceSearchForm:acntInp”).send_keys
“80027309527”
#$driver.find_element(:css,
“td.rich-sb-cell-padding.richfaces_suggestionSelectValue”).click
$driver.find_element(:id, “j_id761:invoiceSearchForm:srchBtn1”).click
$driver.find_element(:id,
“j_id761:invoiceSearchForm:invoiceSearchDataTable:1:eachleminvumnvhvv4B8xt”).click
puts “Searching done based on Entered Account Number”

sleep 1
#$driver.save_screenshot(‘IM.png’)
$driver.find_element(:id, “j_id16:inventoryTab_lbl”).click
sleep 1
$driver.find_element(:id,
“j_id966:provisionInventorySearch:srchBtn”).click
sleep 1
puts “Inventory searched”

$driver.find_element(:id, “j_id16:searchControlTab_lbl”).click
$driver.find_element(:id, “j_id761:invoiceSearchForm:acntnp”).clear
$driver.find_element(:id, “j_id761:invoiceSearchForm:acntInp”).send_keys
“80027309527”
#$driver.find_element(:css,
“td.rich-sb-cell-padding.richfaces_suggestionSelectValue”).click
$driver.find_element(:id, “j_id761:invoiceSearchForm:srchBtn1”).click
$driver.find_element(:id,
“j_id761:invoiceSearchForm:invoiceSearchDataTable:1:eachleminvumnvhvv4B8xt”).click
puts “Searching done based on Entered Account Number”

$driver.find_element(:id, “j_id16:rzrMaintenanceTab_lbl”).click
sleep 1
puts “Supplier list is searched”
@loop = @loop+1
end
#-----------Logging out----------

$driver.switch_to.default_content
sleep 1
puts “Switched back from Iframe”
@check=1
$driver.find_element(:name, “headerForm:logoutLink”).click
puts “User successfully logged out”
end
#-----------Teardown Method… Here i want to catch the failed element
def teardown
if @check==1
$driver.close
puts “passed if condition”
else
puts “passed else condtion”
$driver.save_screenshot(‘IM.png’)
sleep 3

$driver.switch_to.default_content
sleep 1
puts “Switched back from Iframe”

$driver.find_element(:name, “headerForm:logoutLink”).click
puts “User successfully logged out”
sleep 2

$driver.close
puts “test case passed”

  assert_equal [], @verification_errors
  $driver.save_screenshot('IM.png')
puts "test case failed"

end
end

end


#19

Justin C. wrote in post #1129842:

On 12/06/2013 12:43 AM, saurav panda wrote:

Justin,
This is my entire code but u will not have access to use the application
link as it is an internal application.

You left out the important part, which is how you are adding rescue and
what the error is when you try to do so.

-Justin

C:\Users\saurav.panda\Desktop>ruby search__exportEH.rb
Loaded suite search__exportEH
Started
Started InternetExplorerDriver server (64-bit)
2.33.0.0
Listening on port 5555
User Log in
User has successfully logged in
Assertion for IMLink passed
Wait for 5sec for page to load
Switched to Iframe
Wait for 5sec for page load to complete
User Logged in
Searching done based on Entered Account Number
Inventory searched
E

Error: test_Search_Export(Search_Export)
Selenium::WebDriver::Error::NoSuchElementError: Unable to find element
with id == j_id761:invoiceSearchForm:acntnp
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:in
assert_ok' C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:ininitialize’
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in
new' C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:increate_response’
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:in
request' C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:incall’
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in
raw_execute' C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:inexecute’
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:575:in
find_element_by' C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/search_context.rb:42:infind_element’
search__exportEH.rb:62:in `test_Search_Export’
59: puts “Inventory searched”
60:
61: $driver.find_element(:id, “j_id16:searchControlTab_lbl”).click
=> 62: $driver.find_element(:id,
“j_id761:invoiceSearchForm:acntnp”).clear
63: $driver.find_element(:id,
“j_id761:invoiceSearchForm:acntInp”).send_keys “80027309527”
64: #$driver.find_element(:css,
“td.rich-sb-cell-padding.richfaces_suggestionSelectValue”).click
65: $driver.find_element(:id,
“j_id761:invoiceSearchForm:srchBtn1”).click

passed else condtion
Switched back from Iframe
User successfully logged out
test case passed
F

Failure:
test_Search_Export(Search_Export)
search__exportEH.rb:110:in `teardown’
<[]> expected but was

diff:
? []
? nil

Finished in 78.243 seconds.

1 tests, 2 assertions, 1 failures, 1 errors, 0 pendings, 0 omissions, 0
notifications
0% passed

0.01 tests/s, 0.03 assertions/s

C:\Users\saurav.panda\Desktop>


#20

On 12/06/2013 12:57 AM, saurav panda wrote:

C:\Users\saurav.panda\Desktop>ruby search__exportEH.rb
Loaded suite search__exportEH

Allow me to summarize this conversation:

  1. You asked how to get a stack trace
  2. I provided two methods, one using “caller” and the other getting the
    stack trace from an exception
  3. You said you tried to use my code but got an error
  4. I asked for how you tried my code and what the error looked like
  5. You gave me your code WITHOUT any modifications or the error
    mentioned in step 3. However, you did ask a different question, how to
    get a “failed element” in the teardown method
  6. I didn’t see your different question, all I saw was that you didn’t
    have “caller”, “rescue”, or “backtrace” anywhere in the code. So I
    requested the code that included one of these.
  7. You gave me the output of running the webdriver code
  8. Since we seem to be working at different problems, I wrote this list.

Going back to 6, I see what you want has to do with getting information
from webdriver about a failure. You shouldn’t do this in the teardown
method, though, that’s for resetting application state.

However, the information you need is right in the messages output from
webdriver. So at this point I don’t know what you want and we are back
to the beginning.

At this point, I am going to bed. I hate to do this, but I’m going to
have to direct you to http://www.catb.org/~esr/faqs/smart-questions.html
and in particular
http://www.catb.org/~esr/faqs/smart-questions.html#goal

-Justin