A
Ara.T.Howard
what is the meaning of this?
jib:~ > cat a.rb
def method
raise 'foobar'
end
errors = []
2.times do
Thread.new do
begin
method
rescue => e
errors << e
end
end.join
end
p errors
p(errors[-2] == errors[-1])
p((errors[-2].class == errors[-1].class and
errors[-2].message == errors[-1].message and
errors[-2].backtrace == errors[-1].backtrace))
jib:~ > ruby a.rb
[#<RuntimeError: foobar>, #<RuntimeError: foobar>]
false
true
i'm writing some code which must be fault tolerant: something like
def error_wrap errors = [], &block
begin
block.call errors
rescue => e
errors << e
if errors.size >= @fault_tolerance
raise
else
if errors.size == 1 or (errors.size >= 2 and errors[-2] != errors[-1])
@logger.warn{ errors.last }
error_wrap errors, &block
end
end
end
end
which runs a block of code, handling errors by logging them if it's a new
error (don't busy the log duplicate errors), eventually failing if too many
errors are seen.
the problems is that
errors[-2] != errors[-1]
is always true. as the first bit of code shows, the '==' operator seems to be
implemented in a counter intuitive way. doesn't it make sense that exceptions
with the same class, message, and backtrace should be considered the same?
looking briefly at error.c leads me to believe that it's Object#== used to
compare exceptions - does this make sense?
regards.
-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
===============================================================================
jib:~ > cat a.rb
def method
raise 'foobar'
end
errors = []
2.times do
Thread.new do
begin
method
rescue => e
errors << e
end
end.join
end
p errors
p(errors[-2] == errors[-1])
p((errors[-2].class == errors[-1].class and
errors[-2].message == errors[-1].message and
errors[-2].backtrace == errors[-1].backtrace))
jib:~ > ruby a.rb
[#<RuntimeError: foobar>, #<RuntimeError: foobar>]
false
true
i'm writing some code which must be fault tolerant: something like
def error_wrap errors = [], &block
begin
block.call errors
rescue => e
errors << e
if errors.size >= @fault_tolerance
raise
else
if errors.size == 1 or (errors.size >= 2 and errors[-2] != errors[-1])
@logger.warn{ errors.last }
error_wrap errors, &block
end
end
end
end
which runs a block of code, handling errors by logging them if it's a new
error (don't busy the log duplicate errors), eventually failing if too many
errors are seen.
the problems is that
errors[-2] != errors[-1]
is always true. as the first bit of code shows, the '==' operator seems to be
implemented in a counter intuitive way. doesn't it make sense that exceptions
with the same class, message, and backtrace should be considered the same?
looking briefly at error.c leads me to believe that it's Object#== used to
compare exceptions - does this make sense?
regards.
-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
===============================================================================