I've been searching & reading, but not finding (or perhaps just not
understanding) how I can capture as a string the exact same error
message text that would normally get dumped to Terminal.
So, if I write this to generate an error:
puts "hello"
bogus_command
I'll get a file name, line number, and description of an error dumped to
the Terminal.
How do I snag all that text so I can add it to a log file?
Obviously, I'd start with a begin/rescue and inside rescue I'd do
something. I've fiddled with some examples I've seen but nothing is
doing what I want. I just want that same text captured as a string into
a variable I can then use however I want.
begin
puts "hello"
bogus_command
rescue
# now what?
# I was hoping this would work
# x = $stderr.print
# but it doesn't
end
I'm sure it's some standard *nix thing, but I'm not getting it.
I've been searching & reading, but not finding (or perhaps just not
understanding) how I can capture as a string the exact same error
message text that would normally get dumped to Terminal.
begin
puts "hello"
bogus_command
rescue
# now what?
# I was hoping this would work
# x = $stderr.print
# but it doesn't
end
DOH!! As soon as I hit send, I realized I could try one more thing:
x = $!
That gave me the error message. And then a key word popped into my head.
I wanted the "stack trace." More googling gave me the .backtrace method.
So now I have:
begin
puts "hello"
bogus_command
rescue=> err
x = "#{err.backtrace[0] #{err}}"
end
This works perfectly. So, unless there's A Better Way someone wants to
suggest, I think I am good.
I've been searching & reading, but not finding (or perhaps just not
understanding) how I can capture as a string the exact same error
message text that would normally get dumped to Terminal.
So, if I write this to generate an error:
puts "hello"
bogus_command
I'll get a file name, line number, and description of an error dumped
to the Terminal.
How do I snag all that text so I can add it to a log file?
Obviously, I'd start with a begin/rescue and inside rescue I'd do
something. I've fiddled with some examples I've seen but nothing is
doing what I want. I just want that same text captured as a string
into a variable I can then use however I want.
begin
puts "hello"
bogus_command
rescue
# now what?
# I was hoping this would work
# x = $stderr.print
# but it doesn'truby -e"puts X" 2>data
end
I'm sure it's some standard *nix thing, but I'm not getting it.