Exception: Svelte::Errors::TemplateError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/svelte/errors.rb

Constant Summary collapse

SOURCE_CODE_RADIUS =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, error) ⇒ TemplateError

Returns a new instance of TemplateError.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/svelte/errors.rb', line 59

def initialize(template, error)
  raise("Did not provide cause error") unless error
  @cause = error

  raise("Did not provide template") unless template
  @template, @sub_templates = template, nil

  raise("Cause error is nil for TemplateError") unless @cause
  @cause.template = template

  # steep:ignore:start
  super(@cause.message) # : self
  # steep:ignore:end
end

Instance Attribute Details

#causeObject (readonly)

Returns the value of attribute cause.



55
56
57
# File 'lib/svelte/errors.rb', line 55

def cause
  @cause
end

#templateObject (readonly)

Returns the value of attribute template.



55
56
57
# File 'lib/svelte/errors.rb', line 55

def template
  @template
end

Instance Method Details

#annotated_source_codeObject



78
79
80
# File 'lib/svelte/errors.rb', line 78

def annotated_source_code
  @cause.annotated_source_code
end

#backtraceObject

Following is copypasta-ed from ActionView::Template::Error



83
84
85
# File 'lib/svelte/errors.rb', line 83

def backtrace
  @cause.backtrace
end

#backtrace_locationsObject



87
88
89
# File 'lib/svelte/errors.rb', line 87

def backtrace_locations
  @cause.backtrace_locations
end

#file_nameObject



91
92
93
# File 'lib/svelte/errors.rb', line 91

def file_name
  @template.identifier
end

#line_numberObject



124
125
126
127
128
129
130
# File 'lib/svelte/errors.rb', line 124

def line_number
  @line_number ||=
    if file_name
      regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/
      $1 if message =~ regexp || backtrace.find { |line| line =~ regexp }
    end
end

#messageObject



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

def message
  @cause.message
end

#source_extract(indentation = 0) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/svelte/errors.rb', line 104

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

  source_code = @template.encode!.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 = end_on_line.to_s.size + indentation
  return [] unless (source_code = source_code[start_on_line..end_on_line])

  formatted_code_for(source_code, start_on_line, indent)
end

#sub_template_messageObject



95
96
97
98
99
100
101
102
# File 'lib/svelte/errors.rb', line 95

def sub_template_message
  if @sub_templates
    "Trace of template inclusion: " +
      @sub_templates.collect(&:inspect).join(", ")
  else
    ""
  end
end

#sub_template_of(template_path) ⇒ Object



119
120
121
122
# File 'lib/svelte/errors.rb', line 119

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