Exception: Exception

Defined in:
lib/dolzenko/error_print.rb

Overview

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

Port of MRI native ‘error_print` function.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.error_print(e = $ERROR_INFO) ⇒ Object



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
68
69
# File 'lib/dolzenko/error_print.rb', line 12

def self.error_print(e = $ERROR_INFO)
  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

Instance Method Details

#error_printObject



8
9
10
# File 'lib/dolzenko/error_print.rb', line 8

def error_print
  self.class.error_print(self)
end