On Sep 1, 8:49 am, Joel VanderWerf <[email protected]> wrote:
> This patch preserves more of the information carried by exceptions that
> bubble up from particular database libraries and are handled by sequel,
> so that you don't have to parse and match message strings.
>
> The basic idea, abstracted from Sequel, is this:
>
> class DatabaseError < StandardError; end
>
> def raise_error(ex)
> raise DatabaseError, ex, ex.backtrace
> end
>
> def foo
> raise ArgumentError, "bad args"
> rescue => ex
> raise_error(ex)
> end
>
> begin
> foo
> rescue => ex
> puts "CLASS:"
> puts ex.message.class # ==> ArgumentError
> puts "MESSAGE:"
> puts ex.message # ==> bad args
> puts "AS RAISED:"
> raise # raise_error.rb:8:in `foo': bad args
> (DatabaseError)
> # from raise_error.rb:14
> end
>
> This uses two tricks in ruby exception handling:
>
> 1. The message can be any object.
>
> 2. The third argument to raise can be a backtrace (that's not important
> for this exercise, though).
I didn't know that raise took a third argument. That may make the
implementation simpler. Thanks for expanding my ruby knowledge.
I don't think making the message the underlying exception is a good
idea, as it will break too much existing code. message indicates a
string, and virtually all ruby programmers will expect that exception
messages are strings.
I'm planning on adding a Sequel.convert_exception_class(exception,
new_class) method that parts of Sequel will call to convert underlying
exceptions to Sequel's exceptions (usually DatabaseError). You will
be able to override that to get the behavior that you want.
We could expand Sequel::Error to store the underlying exception class
(if any):
class Sequel::Error
attr_accessor :underlying_exception
end
That way all Sequel::Errors would have it, and you'd be able to match
on the underlying exception, plus it wouldn't break assumptions about
message being a string. What do you think of that?
Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---