Hiding app.run in begin-rescue block: pros & cons?

R

Richard Lionheart

Hi All,

Upon asking a friend to review my code, he suggested that many people hide
app.run in a begin-rescue block. I assume one might code:

begin
app.run
rescue => err
puts "Application failed:", err
end

(which I got from Hal Fulton's "The Ruby Way")

What are the pros and cons of this approach?

Regards,
Richard
 
R

Richard Lionheart

Hi All,

I apologize for not testing the two approaches myself before posting the
question. My take on this now is:

Use begin-rescue when code is released for production, because it prevents
a "blizzard" of trace-back messages which would dismay users.

Use plain app.run when in development mode, because the trace-back is
generally helpful.

Anyone have other ideas?

Regards,
Richard
 
A

Ara.T.Howard

Hi All,

I apologize for not testing the two approaches myself before posting the
question. My take on this now is:

Use begin-rescue when code is released for production, because it prevents
a "blizzard" of trace-back messages which would dismay users.

Use plain app.run when in development mode, because the trace-back is
generally helpful.

Anyone have other ideas?

Regards,
Richard


require 'logger'


logger = Logger.new STDOUT

....
....


begin
app.run
rescue Exception => e
logger.debug{ e }
logger.fatal{ 'application failed' }
end


print stack trace only when debugging, give users the ability to turn
debugging on with a command line switch.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
N

NAKAMURA, Hiroshi

Hi,

Ara.T.Howard said:
require 'logger'
logger = Logger.new STDOUT
begin
app.run
rescue Exception => e
logger.debug{ e }
logger.fatal{ 'application failed' }
end


print stack trace only when debugging, give users the ability to turn
debugging on with a command line switch.

And here's a sample of class Logger::Application which might be usable
for original purpose?

Regards,
// NaHi

require 'logger'

class App < Logger::Application
def initialize(level)
super
@log.level = Logger.const_get(level.upcase)
end

def run
@log.debug { "debug msg" }
@log.info { "info msg" }
if @log.fatal? # true iif fatal msg is allowed to dump
raise # an exception is logged as a fatal msg
end
0 # exit status
end
end

# test

puts "<<DEBUG"
p App.new("DEBUG").start
puts "DEBUG"

puts

puts "<<INFO"
p App.new("INFO").start
puts "INFO"

puts

puts "<<UNKNOWN"
p App.new("UNKNOWN").start
puts "UNKNOWN"
 
D

daz

And here's a sample of class Logger::Application [...]

[snip useful sample]


Plenty nice !!

Very well documented in <rubydir>/lib/logger.rb -AND/OR- ...
<http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/lib/logger.rb?rev=1.5.2.4;content-type=text/plain>


<<DEBUG
I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: Start of DEBUG.
D, [2004-07-16T04:45:30.320000 #613397] DEBUG -- DEBUG: debug msg
I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: info msg
F, [2004-07-16T04:45:30.320000 #613397] FATAL -- DEBUG: Detected an exception. Stopping ... (RuntimeError)
C:/TEMP/rb1022.TMP:13:in `run'
D:/RUBY/SRC_CVSINST/lib/ruby/1.9/logger.rb:684:in `start'
C:/TEMP/rb1022.TMP:22
I, [2004-07-16T04:45:30.320000 #613397] INFO -- DEBUG: End of DEBUG. (status: -1)
-1
DEBUG

<<INFO
I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: Start of INFO.
I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: info msg
F, [2004-07-16T04:45:30.430000 #613397] FATAL -- INFO: Detected an exception. Stopping ... (RuntimeError)
C:/TEMP/rb1022.TMP:13:in `run'
D:/RUBY/SRC_CVSINST/lib/ruby/1.9/logger.rb:684:in `start'
C:/TEMP/rb1022.TMP:28
I, [2004-07-16T04:45:30.430000 #613397] INFO -- INFO: End of INFO. (status: -1)
-1
INFO

<<UNKNOWN
0
UNKNOWN

Regards,
// NaHi

:daz
 
A

Alexey Verkhovsky

irb(main):001:0> s = "#{aaa}"
NameError: undefined local variable or method `aaa' for main:Object
from (irb):1
from :0
irb(main):002:0> s = %s{#{aaa}}
/usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
method `ancestors' for nil:NilClass (NoMethodError)
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
`identify_string'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:914:in
`identify_quotation'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:667:in `lex_int2'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:664:in `call'
from /usr/local/lib/ruby/1.9/irb/slex.rb:234:in `match_io'
from /usr/local/lib/ruby/1.9/irb/slex.rb:219:in `match_io'
from /usr/local/lib/ruby/1.9/irb/slex.rb:73:in `match'
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:277:in `token'
... 7 levels...
from /usr/local/lib/ruby/1.9/irb.rb:70:in `start'
from /usr/local/lib/ruby/1.9/irb.rb:69:in `catch'
from /usr/local/lib/ruby/1.9/irb.rb:69:in `start'
from /usr/local/bin/irb:13
[alex@dhcp-89-2 alex]$ ruby -v
ruby 1.9.0 (2004-07-08) [i686-linux]

------

Meantime,

[alex@dhcp-89-2 alex]$ ruby
s = %s{#{aaa}}
p s
:"\#{aaa}"
 
Y

Yukihiro Matsumoto

Hi,

In message "Bug? String literal %s{#{a}} cause irb to abort"

|irb(main):002:0> s = %s{#{aaa}}
|/usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
|method `ancestors' for nil:NilClass (NoMethodError)
| from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
|`identify_string'
| from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:914:in
|`identify_quotation'

It's a bug in irb. Thank you for the report.

matz.
 
N

nobu.nokada

Hi,

At Sat, 17 Jul 2004 09:05:08 +0900,
Alexey Verkhovsky wrote in [ruby-talk:106691]:
irb(main):001:0> s = "#{aaa}"
NameError: undefined local variable or method `aaa' for main:Object
from (irb):1
from :0
irb(main):002:0> s = %s{#{aaa}}
/usr/local/lib/ruby/1.9/irb/ruby-token.rb:101:in `Token': undefined
method `ancestors' for nil:NilClass (NoMethodError)
from /usr/local/lib/ruby/1.9/irb/ruby-lex.rb:995:in
`identify_string'

Does this help?


Index: lib/irb/ruby-lex.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/irb/ruby-lex.rb,v
retrieving revision 1.22
diff -U2 -p -d -r1.22 ruby-lex.rb
--- lib/irb/ruby-lex.rb 4 Oct 2003 17:51:09 -0000 1.22
+++ lib/irb/ruby-lex.rb 17 Jul 2004 04:35:34 -0000
@@ -325,4 +325,5 @@ class RubyLex
"\`" => TkDXSTRING,
"\/" => TkDREGEXP,
+ ":" => TkDSYMBOL,
}

@@ -974,5 +975,5 @@ class RubyLex
if @quoted == ch and nest == 0
break
- elsif @ltype != "'" && @ltype != "]" and ch == "#"
+ elsif ! "']:".index(@ltype) and ch == "#"
subtype = true
elsif ch == '\\' #'
Index: lib/irb/ruby-token.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/irb/ruby-token.rb,v
retrieving revision 1.5
diff -U2 -p -d -r1.5 ruby-token.rb
--- lib/irb/ruby-token.rb 5 Aug 2003 03:08:16 -0000 1.5
+++ lib/irb/ruby-token.rb 17 Jul 2004 04:35:26 -0000
@@ -170,4 +170,5 @@ module RubyToken
[:TkDXSTRING, TkNode],
[:TkDREGEXP, TkNode],
+ [:TkDSYMBOL, TkNode],
[:TkNTH_REF, TkNode],
[:TkBACK_REF, TkNode],
 
A

Alexey Verkhovsky

Does this help?
... diff patch to lib/irb/ruby-lex.rb and lib/irb/ruby-token.rb ...

Hi,

The bug I reported earlier is not reproduced, but there is some other
interesting behavior (three years I've spent in system test just won't
go away :))

[alex@dhcp-89-2 ruby]$ irb
irb(main):001:0> a = %s{#"aaa"} // previously reported
=> :"\#\"aaa\"" // now behaves as expected

// Another odd behavior
irb(main):002:0> a = %s
irb(main):003:0:
// expected: SyntaxError
// actual: an "empty symbol" value is assigned to a

irb(main):004:0> a
SyntaxError: compile error
(irb):3: empty symbol literal
(irb):4: syntax error
from (irb):4
from :0
// expected: SyntaxError
irb(main):009:0> a.inspect
SyntaxError: compile error
(irb):8: empty symbol literal
(irb):9: syntax error
a.inspect
^
from (irb):9
from :0
irb(main):010:0> a.class
=> Symbol


// empty symbol is illegal
irb(main):011:0> a = %s{}
SyntaxError: compile error
(irb):11: empty symbol literal
from (irb):11
from :0


Best regards,
Alexey Verkhovsky
 
N

nobu.nokada

Hi,

At Sat, 17 Jul 2004 22:40:34 +0900,
Alexey Verkhovsky wrote in [ruby-talk:106711]:
// Another odd behavior
irb(main):002:0> a = %s
irb(main):003:0:
// expected: SyntaxError
// actual: an "empty symbol" value is assigned to a

It is actually same as:

$ ruby -e '%s' -e ''
-e:2: empty symbol literal

i.e., a symbol litral terminated by \n.
irb(main):004:0> a
SyntaxError: compile error

But I don't know why it delays until the next line; this would
be an irb issue.
 
A

Alexey Verkhovsky

But I don't know why it delays until the next line;

You are right, it doesn't assign anything to the variable, just delays
reporting of the problem. E.g.

[alex@dhcp-89-2 ruby]$ irb
irb(main):001:0> a=%s
irb(main):002:0:
irb(main):003:0> puts "aaa"
SyntaxError: compile error
(irb):2: empty symbol literal
(irb):3: syntax error
puts "aaa"
^
from (irb):3
from :0
 
M

Mark Hubbart

But I don't know why it delays until the next line;

You are right, it doesn't assign anything to the variable, just delays
reporting of the problem. E.g.

[alex@dhcp-89-2 ruby]$ irb
irb(main):001:0> a=%s
irb(main):002:0:
irb(main):003:0> puts "aaa"
SyntaxError: compile error
(irb):2: empty symbol literal
(irb):3: syntax error
puts "aaa"
^
from (irb):3
from :0

That's because newlines are valid delimiters in that context:

irb(main):009:0> a=%s
irb(main):010:0: testing
irb(main):011:0>
=> :testing

It delays because it's waiting for you to close out the literal.

What you typed above is equivalent to this: a=%s{} puts "aaa"; so it
give two errors: a compile error for the empty symbol, and a syntax
error because to can't put puts where it is :)

cheers,
Mark
 
A

Alexey Verkhovsky

That's because newlines are valid delimiters in that context:

Aha... now I understand.
To take this hairsplitting exercise a little further, there is a slight
difference between ruby and irb here:

[alex@dhcp-89-2 alex]$ ruby
%s

-:2: empty symbol literal
[alex@dhcp-89-2 alex]$

[alex@dhcp-89-2 alex]$ irb
irb(main):001:0> %s
irb(main):002:0:
irb(main):003:0>
SyntaxError: compile error
(irb):2: empty symbol literal
from (irb):3
from :0
irb(main):004:0>

So, ruby sees an error after two linefeeds, while irb does it only after
three.

Although, if I make a file out string "%s\n\n", both interpreters behave
the same on it...

I hope everybody can live with this :)

Best regards,
Alex
 
M

Mark Hubbart

That's because newlines are valid delimiters in that context:

Aha... now I understand.
To take this hairsplitting exercise a little further,
:)

there is a slight
difference between ruby and irb here:

[alex@dhcp-89-2 alex]$ ruby
%s

-:2: empty symbol literal
[alex@dhcp-89-2 alex]$

[alex@dhcp-89-2 alex]$ irb
irb(main):001:0> %s
irb(main):002:0:
irb(main):003:0>
SyntaxError: compile error
(irb):2: empty symbol literal
from (irb):3
from :0
irb(main):004:0>

So, ruby sees an error after two linefeeds, while irb does it only
after
three.

In this test, I used a plus sign to indicate that the statement should
continue to the next line:

mark@eMac% ruby
%s{} +
-:1: empty symbol literal
%s{} +
^

I think irb waits on the compilation until it gets a full statement;
while ruby compiles at the smallest expression level (but still
line-based). So, while irb will wait to see if you try to add anything
after that empty symbol literal, ruby grabs it right away and shows you
where you went wrong. :)
I hope everybody can live with this :)

cheers,
Mark
 
A

Alexey Verkhovsky

I think irb waits on the compilation until it gets a full statement;
while ruby compiles at the smallest expression level (but still
line-based).

Thanks for the explanation. Acquiring such subtle understandings is what
makes hair-splitting exercises more than merely amusing.

:)

Alex
 
R

Richard Lionheart

Thank you Ara, NaHi and Daz for the guidance and the references. My Ruby
education continues! I'll check this out further and hopefully have no more
questions about it.

Best wishes,
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top