Exception: Lookbook::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Lookbook::Error
- Defined in:
- lib/lookbook/error.rb
Constant Summary collapse
- LINES_AROUND =
3
Instance Method Summary collapse
- #file_lang ⇒ Object
- #file_name ⇒ Object
- #file_path ⇒ Object
-
#initialize(original = nil, title: nil, message: nil, file_path: nil, file_name: nil, line_number: nil, source_code: nil) ⇒ Error
constructor
A new instance of Error.
- #line_number ⇒ Object
- #message ⇒ Object
- #parsed_backtrace ⇒ Object
- #source_code ⇒ Object
- #source_code_lines ⇒ Object
- #title ⇒ Object
Constructor Details
#initialize(original = nil, title: nil, message: nil, file_path: nil, file_name: nil, line_number: nil, source_code: nil) ⇒ Error
Returns a new instance of Error.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/lookbook/error.rb', line 7 def initialize(original = nil, title: nil, message: nil, file_path: nil, file_name: nil, line_number: nil, source_code: nil) @original = original @title = title @message = @file_path = file_path @file_name = file_name @line_number = line_number @source_code = source_code super() end |
Instance Method Details
#file_lang ⇒ Object
56 57 58 59 |
# File 'lib/lookbook/error.rb', line 56 def file_lang lang = Lookbook::Lang.guess(file_path) lang.present? ? lang[:name] : "plaintext" end |
#file_name ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/lookbook/error.rb', line 69 def file_name if @file_name == false nil else @file_name || file_path end end |
#file_path ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/lookbook/error.rb', line 77 def file_path path = if @file_path.nil? parsed_backtrace[0][0] if parsed_backtrace.any? else @file_path.presence || nil end path.nil? ? nil : path.to_s.delete_prefix("#{Rails.root}/") end |
#line_number ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/lookbook/error.rb', line 86 def line_number number = if @line_number.nil? parsed_backtrace[0][1] if parsed_backtrace.any? else @line_number.presence || nil end number.present? ? number.to_i : number end |
#message ⇒ Object
65 66 67 |
# File 'lib/lookbook/error.rb', line 65 def (@message || target.).gsub("(<unknown>):", "").strip.upcase_first end |
#parsed_backtrace ⇒ Object
95 96 97 98 99 100 |
# File 'lib/lookbook/error.rb', line 95 def parsed_backtrace backtrace.map do |x| x =~ /^(.+?):(\d+)(|:in `(.+)')$/ [$1, $2, $4] end end |
#source_code ⇒ Object
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 |
# File 'lib/lookbook/error.rb', line 18 def source_code lines = source_code_lines if lines.present? && line_number.is_a?(Integer) start_line = source_code_start_line(lines) end_line = source_code_end_line(lines) highlighted_line = source_code_highlighted_line(lines) line_count = end_line - start_line relevant_lines = lines.slice(start_line - 1, line_count + 1) if relevant_lines.present? empty_start_lines = 0 relevant_lines.each do |line| break unless line.strip.empty? empty_start_lines += 1 end { code: relevant_lines.join("\n").lstrip, start_line: start_line, highlighted_line: highlighted_line - empty_start_lines } end end end |
#source_code_lines ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lookbook/error.rb', line 45 def source_code_lines if file_path || @source_code if @source_code @source_code.split("\n") else full_path = Rails.root.join(file_path) File.read(full_path).split("\n") if File.exist? full_path end end end |
#title ⇒ Object
61 62 63 |
# File 'lib/lookbook/error.rb', line 61 def title @title || target.class.to_s end |