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).

Note that the default output of raise doesn't show the wrapped exception 
class, but you can easily get it displayed using message.inspect or 
message.class.

The value of this, IMO, is that you can do

case ex.message
when SomeWrappedException
...
end

instead of having to parse the message string.

This works as a monkey patch, or as the patch below. It will break code 
if the code was written to depend on message strings having certain 
contents (which is probably a bad idea anyway).

---
sequel-3.3.0/lib/sequel/database.rb     2009-08-03 10:06:28.000000000 -0700
+++ -   2009-09-01 08:40:08.340063788 -0700
@@ -792,7 +792,7 @@
      # and traceback.
      def raise_error(exception, opts={})
        if !opts[:classes] || Array(opts[:classes]).any?{|c| 
exception.is_a?(c)}
-        e = (opts[:disconnect] ? DatabaseDisconnectError : 
DatabaseError).new("#{exception.class}: #{exception.message}")
+        e = (opts[:disconnect] ? DatabaseDisconnectError : 
DatabaseError).new(exception)
          e.set_backtrace(exception.backtrace)
          raise e
        else

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to