Module: Justified::Error

Included in:
StandardError
Defined in:
lib/justified.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) calculate_same_line_count(backtrace, cause_backtrace) (private)



74
75
76
# File 'lib/justified.rb', line 74

def calculate_same_line_count(backtrace, cause_backtrace)
  backtrace.reverse.zip(cause_backtrace.reverse).select { |a, b| a == b }.size
end

- (Exception) cause

Cause of the exception

Returns:

  • (Exception)

    cause of the exception



24
25
26
# File 'lib/justified.rb', line 24

def cause
  @cause
end

- (Object) cause=(exception)

Set cause of the exception

Parameters:

  • exception (nil, Exception)


18
19
20
21
# File 'lib/justified.rb', line 18

def cause=(exception)
  set_cause exception
  set_backtrace
end

- (Object) initialize(message = nil, cause = $!)



10
11
12
13
14
# File 'lib/justified.rb', line 10

def initialize(message = nil, cause = $!)
  super(message)
  set_cause cause
  @backtrace = nil
end

- (Object) recalculate_rest(rest, same_line_count) (private)



66
67
68
69
70
71
72
# File 'lib/justified.rb', line 66

def recalculate_rest(rest, same_line_count)
  rest.map do |line|
    line.gsub(/^#{SKIP_STR % '(\d+)'}$/) do
      SKIP_STR % ($1.to_i - same_line_count)
    end
  end
end

- (Object) set_backtrace(backtrace = nil)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/justified.rb', line 28

def set_backtrace(backtrace = nil)
  @backtrace ||= backtrace
  backtrace  ||= @backtrace

  if backtrace
    unless cause
      super backtrace
    else
      cause_backtrace, rest   = split_cause_backtrace
      same_line_count         = calculate_same_line_count backtrace, cause_backtrace
      cause_backtrace_striped = cause_backtrace[0..-(same_line_count+1)]

      super backtrace +
                ["#{CAUSED_STR} (#{cause.class}) #{cause.message}"] +
                cause_backtrace_striped +
                [SKIP_STR % same_line_count] +
                recalculate_rest(rest, same_line_count)
    end
  end
end

- (Object) set_cause(cause) (private)



51
52
53
54
55
# File 'lib/justified.rb', line 51

def set_cause(cause)
  cause.nil? or exception.kind_of? Exception or
      raise ArgumentError, 'cause must be kind of Exception'
  @cause = cause
end

- (Object) split_cause_backtrace (private)



57
58
59
60
61
62
63
64
# File 'lib/justified.rb', line 57

def split_cause_backtrace
  _, cause_index = cause.backtrace.each_with_index.find { |line, _| line =~ /^#{CAUSED_STR}/ }
  if cause_index
    [cause.backtrace[0...cause_index], cause.backtrace[cause_index..-1]]
  else
    [cause.backtrace, []]
  end
end