deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [1/3] git commit: Write exceptions captured by DSL to Logger
Date Wed, 26 Sep 2012 21:12:37 GMT
Updated Branches:
  refs/heads/master 96a2fc0ab -> 779cc3908


Write exceptions captured by DSL to Logger

Instead of using $stderr directly, use Ruby
Logger class to wrap them. In future it will be
easier to redirect to 'errors.log'.


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/779cc390
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/779cc390
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/779cc390

Branch: refs/heads/master
Commit: 779cc3908d8e4dbd66133a9966810009bfab122e
Parents: 2ffef85
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Sat Sep 22 00:29:57 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Wed Sep 26 23:09:54 2012 +0200

----------------------------------------------------------------------
 server/lib/deltacloud/drivers/exceptions.rb |   33 ++++++++++++----------
 1 files changed, 18 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/779cc390/server/lib/deltacloud/drivers/exceptions.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/exceptions.rb b/server/lib/deltacloud/drivers/exceptions.rb
index 368891a..dba94b6 100644
--- a/server/lib/deltacloud/drivers/exceptions.rb
+++ b/server/lib/deltacloud/drivers/exceptions.rb
@@ -131,7 +131,7 @@ module Deltacloud
 
       # Condition can be class or regexp
       #
-      def match?(e)
+      def any?(e)
         @conditions.each do |c|
           return true if c.class == Class && e.class == c
           return true if c.class == Regexp && (e.class.name =~ c or e.message =~
c)
@@ -180,10 +180,16 @@ module Deltacloud
     module DSL
       def exceptions(&block)
         @definitions = Exceptions.new(&block).exception_definitions if block_given?
+        @definitions ||= []
         @definitions
       end
     end
 
+    def self.logger(logger=nil)
+      @logger = logger
+      @logger ||= ::Logger.new($stderr)
+    end
+
     def self.included(klass)
       klass.extend(DSL)
     end
@@ -191,22 +197,19 @@ module Deltacloud
     def safely(&block)
       begin
         block.call
-      rescue
-        report_method = $stderr.respond_to?(:err) ? :err : :puts
-        self.class.exceptions.each do |exdef|
-          if exdef.match?($!)
-            new_exception = exdef.handler($!)
-            m = (new_exception && !new_exception.message.nil?) ? new_exception.message
: $!.message
-            unless ENV['RACK_ENV'] == 'test'
-              $stderr.send(report_method, "#{[$!.class.to_s, m].join(':')}\n#{$!.backtrace[0..10].join("\n")}")
-            end
-            raise exdef.handler($!) unless new_exception.nil?
+      rescue => e
+        log = ExceptionHandler.logger
+        self.class.exceptions.each do |definitions|
+          next unless definitions.any? e
+          if (new_exception = definitions.handler(e)) and new_exception.message
+            message = new_exception.message
           end
+          message ||= e.message
+          log.error "#{[e.class.to_s, message].join(':')}\n#{e.backtrace[0..10].join("\n")}"
unless ENV['RACK_ENV'] == 'test'
+          raise definitions.handler(e) unless new_exception.nil?
         end
-        unless ENV['RACK_ENV'] == 'test'
-          $stderr.send(report_method, "[NO HANDLED] #{[$!.class.to_s, $!.message].join(':
')}\n#{$!.backtrace.join("\n")}")
-        end
-        raise Deltacloud::ExceptionHandler::BackendError.new($!, "Unhandled exception or
status code (#{$!.message})")
+        log.error "[NO HANDLED] #{[e.class.to_s, e.message].join(': ')}\n#{e.backtrace.join("\n")}"
unless ENV['RACK_ENV'] == 'test'
+        raise BackendError.new(e, "Unhandled exception or status code (#{e.message})")
       end
     end
 


Mime
View raw message