Exception: Exception

Defined in:
lib/core/facets/exception/detail.rb,
lib/core/facets/exception/raised.rb,
lib/core/facets/exception/suppress.rb,
lib/core/facets/exception/error_print.rb,
lib/core/facets/exception/set_message.rb

Direct Known Subclasses

Lazy::DivergenceError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.error_print(e) ⇒ Object

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/core/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?Boolean

Does a block raise an a given exception.

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
# File 'lib/core/facets/exception/raised.rb', line 5

def self.raised? #:yeild:
  begin
    yield
    false
  rescue self
    true
  end
end

.suppress(*exception_classes) ⇒ Object

Supress errors while executing a block, with execptions.

CREDIT: David Heinemeier Hansson, Thomas Sawyer



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/core/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

Instance Method Details

#detailObject

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

Author:

  • George Moschovitis



8
9
10
11
12
13
14
# File 'lib/core/facets/exception/detail.rb', line 8

def detail
  if backtrace
    %{#{self.class.name}: #{message}\n  #{backtrace.join("\n  ")}\n  LOGGED FROM: #{caller[0]}}
  else
    %{#{self.class.name}: #{message}\n  LOGGED FROM: #{caller[0]}}
  end
end

#error_printObject

TODO:

Anyone have a better name for this method?

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

Author:

  • Evgeniy Dolzhenko



75
76
77
# File 'lib/core/facets/exception/error_print.rb', line 75

def error_print
  Exception.error_print(self)
end

#set_message(string) ⇒ Object

See exception message.



5
6
7
# File 'lib/core/facets/exception/set_message.rb', line 5

def set_message(string)
  @mesg = string.to_s
end