module Deltacloud::ExceptionHandler

Public Class Methods

exceptions(&block) click to toggle source
# File lib/deltacloud/drivers/exceptions.rb, line 174
def self.exceptions(&block)
  @definitions = Exceptions.new(&block).exception_definitions if block_given?
  @definitions
end

Public Instance Methods

safely(&block) click to toggle source
# File lib/deltacloud/drivers/exceptions.rb, line 179
def safely(&block)
  begin
    block.call
  rescue
    report_method = $stderr.respond_to?(:err) ? :err : :puts
    Deltacloud::ExceptionHandler::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?
      end
    end
    $stderr.send(report_method, "[NO HANDLED] #{[$!.class.to_s, $!.message].join(': ')}\n#{$!.backtrace.join("\n")}")
    raise Deltacloud::ExceptionHandler::BackendError.new($!, "Unhandled exception or status code (#{$!.message})")
  end
end