# class Exception

### Public Class Methods

error_print(e) click to toggle source

Formats the Exception so that it looks familiar, i.e. exactly like your interpreter does it.

Port of MRI native `error_print` function.

@author Evgeniy Dolzhenko

    # File lib/facets/exception/error_print.rb, line 10
    def self.error_print(e)
      warn_print = ""
      backtrace = e.backtrace
      backtrace = [ backtrace ] if backtrace.is_a?(String) # 1.9 returns single String for SystemStackError

      warn_print << backtrace[0]
      if e.is_a?(RuntimeError) && e.message.empty?
        warn_print << ": unhandled exception\n"
      else
        if e.message.empty?
          warn_print << ": #{ e.class.name }\n"
        else
          split_message = e.message.split("\n")
          warn_print << ": "
          if split_message.size == 1
            warn_print << "#{ e.message } (#{ e.class.name })\n"
          else
            warn_print << split_message[0]
            warn_print << " (#{ e.class.name })\n"
            warn_print << split_message[1..-1].join("\n").chomp << "\n"
          end
        end
      end

      len = backtrace.size

      ## int skip = eclass == rb_eSysStackError;
      skip = e.is_a?(SystemStackError)

      ## #define TRACE_MAX (TRACE_HEAD+TRACE_TAIL+5)
      ## #define TRACE_HEAD 8
      ## #define TRACE_TAIL 5
      trace_head = 8
      trace_tail = 5
      trace_max = (trace_head + trace_tail + 5)
      ##
      ##  for (i = 1; i < len; i++) {
      i = 1
      while i < len
        ##            if (TYPE(ptr[i]) == T_STRING) {
        ##                warn_printf("\tfrom %s\n", RSTRING_PTR(ptr[i]));
        ##            }
        warn_print << "\tfrom %s\n" % e.backtrace[i]

        ##            if (skip && i == TRACE_HEAD && len > TRACE_MAX) {
        if skip && i == trace_head && len > trace_max
        ##                warn_printf("\t ... %ld levels...\n",
        ##                            len - TRACE_HEAD - TRACE_TAIL);
          warn_print << "\t ... %d levels...\n" % (len - trace_head - trace_tail)
        ##                i = len - TRACE_TAIL;
          i = len - trace_tail
        ##            }
        end
        ##        }
        i += 1
      end
      warn_print
    end

raised?() { || ... } click to toggle source

Does a block raise an a given `exception`.

    # File lib/facets/exception/raised.rb, line 5
    def self.raised? #:yeild:
      begin
        yield
        false
      rescue self
        true
      end
    end

suppress(*exception_classes) { || ... } click to toggle source

Supress errors while executing a block, with execptions.

CREDIT: David Heinemeier Hansson, Thomas Sawyer

    # File lib/facets/exception/suppress.rb, line 7
    def self.suppress(*exception_classes)
      exception_classes.each do |e|
        unless e < self
          raise ArgumentError, "exception #{e} not a subclass of #{self}"
        end
      end
      exception_classes = [self] | exception_classes
      begin
        yield
      rescue Exception => e
        raise unless exception_classes.any? { |cls| e.kind_of?(cls) }
      end
    end

### Public Instance Methods

detail() click to toggle source

Pretty string output of exception/error object useful for helpful debug messages.

@author George Moschovitis

    # File lib/facets/exception/detail.rb, line 8
    def detail
      if backtrace
        %Q{#{self.class.name}: #{message}\n  #{backtrace.join("\n  ")}\n  LOGGED FROM: #{caller[0]}}
      else
        %Q{#{self.class.name}: #{message}\n  LOGGED FROM: #{caller[0]}}
      end
    end

error_print() click to toggle source

Formats the Exception so that it looks familiar, i.e. exactly like your interpreter does it.

@author Evgeniy Dolzhenko

@todo Anyone have a better name for this method?

    # File lib/facets/exception/error_print.rb, line 75
    def error_print
      Exception.error_print(self)
    end