Module: Reactive::TemplateError

Defined in:
lib/reactive-core/errors.rb

Overview

For all kind of errors that may occur in eval’ed code, include this module This allow for a more precise diagnostic

Constant Summary collapse

SOURCE_CODE_RADIUS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_nameObject (readonly)

the last accessor is a duck typing check for TemplateError identification



11
12
13
# File 'lib/reactive-core/errors.rb', line 11

def file_name
  @file_name
end

#original_exceptionObject (readonly)

the last accessor is a duck typing check for TemplateError identification



11
12
13
# File 'lib/reactive-core/errors.rb', line 11

def original_exception
  @original_exception
end

#template_errorObject (readonly)

the last accessor is a duck typing check for TemplateError identification



11
12
13
# File 'lib/reactive-core/errors.rb', line 11

def template_error
  @template_error
end

Instance Method Details

#backtraceObject

don’t do anything nontrivial here. Any raised exception from here becomes fatal (and can’t be rescued).



74
75
76
# File 'lib/reactive-core/errors.rb', line 74

def backtrace
  @backtrace
end

#clean_backtraceObject



22
23
24
# File 'lib/reactive-core/errors.rb', line 22

def clean_backtrace
  original_exception.clean_backtrace
end

#initialize(file_name, source_code, original_exception) ⇒ Object



13
14
15
16
# File 'lib/reactive-core/errors.rb', line 13

def initialize(file_name, source_code, original_exception)
  @file_name, @source_code, @original_exception = file_name, source_code, original_exception
  @backtrace = compute_backtrace
end

#line_numberObject



58
59
60
61
62
63
64
65
# File 'lib/reactive-core/errors.rb', line 58

def line_number
  @line_number ||=
    if file_name
      regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/

      $1 if message =~ regexp or clean_backtrace.find { |line| line =~ regexp }
    end
end

#messageObject



18
19
20
# File 'lib/reactive-core/errors.rb', line 18

def message
  ActiveSupport::Deprecation.silence { original_exception.message }
end

#source_extract(indentation = 0) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/reactive-core/errors.rb', line 34

def source_extract(indentation = 0)
  return unless num = line_number
  num = num.to_i

  source_code = @source_code.split("\n")

  start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
  end_on_line   = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min

  indent = ' ' * indentation
  line_counter = start_on_line
  return unless source_code = source_code[start_on_line..end_on_line]

  source_code.sum do |line|
    line_counter += 1
    "#{indent}#{line_counter}: #{line}\n"
  end
end

#sub_template_messageObject



26
27
28
29
30
31
32
# File 'lib/reactive-core/errors.rb', line 26

def sub_template_message
  if @sub_templates
    "Trace of template inclusion: #{@sub_templates.join(", ")}"
  else
    ""
  end
end

#sub_template_of(template_path) ⇒ Object



53
54
55
56
# File 'lib/reactive-core/errors.rb', line 53

def sub_template_of(template_path)
  @sub_templates ||= []
  @sub_templates << template_path
end

#to_sObject



67
68
69
70
# File 'lib/reactive-core/errors.rb', line 67

def to_s
  "\n\n#{self.class} (#{message}) #{source_location}:\n" +
    "#{source_extract}\n    #{clean_backtrace.join("\n    ")}\n\n"
end