render_me_pretty 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5e982d58b2b59ca91a4713eeb1720ba73fcc0fe6c23ad0052ec54fa8c80adde
4
- data.tar.gz: 2b791ee128231df293c58fa55c064d1cae60df140baf6460c38f68446325f04a
3
+ metadata.gz: 9175cf8f3c94175794b0bc0f5ed0ef8e9620193889d9fb17da206bd1eda235b9
4
+ data.tar.gz: aa14dc5c61c60629ab3e6871a65c9a80e2ec51523e0bfd252aba1f3401949668
5
5
  SHA512:
6
- metadata.gz: '085a42766665d911e15928aa5026d3bc95aa595030a92d20340222b0661750dd5b24ae95884310828b67952cd3b0b697c545d2ef470d5b6ee9a6d811621fb1ba'
7
- data.tar.gz: '0943c0fb992da2d40e00b2d4242c70694bca5195672eea6e4164a19fd32cf505f6918cf9b7e84ed9e0b594c6a47defc8ce9b6e06753c767181ba7c6869b6267d'
6
+ metadata.gz: 40315ff34828109eca1823fa9e4238021fc333d2311d8b22cc0d752b2b962d7cf53114b146737ba2954fcb8d52bbffdaadaae2de9fcf266f56845450dec2ce60
7
+ data.tar.gz: a9067cc69cb6316cd37af1dd0d9175c18402d07c71e33afac2f04a0c31173134fa5247d0f47b87768eae5dbf0d4593ceaaa1a713bf65d289fbb0b1b8e14aef08
@@ -3,6 +3,10 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.6.0]
7
+ - Handle SyntaxError errors correctly
8
+ - refactor into MainErrorHandler and SyntaxError classes
9
+
6
10
  ## [0.5.0]
7
11
  - fix find_template_error_line
8
12
  - remove some of the filtering for backtrace lines
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- render_me_pretty (0.3.0)
4
+ render_me_pretty (0.6.0)
5
5
  activesupport
6
6
  colorize
7
7
  tilt
@@ -43,6 +43,10 @@ require 'tilt/erb'
43
43
 
44
44
  module RenderMePretty
45
45
  class Erb
46
+ autoload :BaseHandler, 'render_me_pretty/erb/base_handler'
47
+ autoload :SyntaxErrorHandler, "render_me_pretty/erb/syntax_error_handler"
48
+ autoload :MainErrorHandler, "render_me_pretty/erb/main_error_handler"
49
+
46
50
  def initialize(path, variables={})
47
51
  @path = path
48
52
  @init_vars = variables
@@ -76,47 +80,28 @@ module RenderMePretty
76
80
  end
77
81
  end
78
82
 
79
- # handles Tilt error
80
- def handle_exception(e)
81
- error_line = find_template_error_line(e.backtrace)
82
- error_line_number = error_line.split(':')[1].to_i
83
- pretty_error(e, error_line_number)
84
- end
85
-
86
- # For Tilt, first line of the baacktrace that contains the path of the file
87
- # we're rendeirng has the line number. Example:
83
+ # Handles Tilt error in prettier manner.
84
+ #
85
+ # When there's a syntax error Tilt does not include the line nubmer of the
86
+ # error in the backtrace it is instead included in the e.message itself.
87
+ #
88
+ # When for other errors the line_number is included in the backtrace.
88
89
  #
89
- # spec/fixtures/invalid.erb:2:in `block in singleton class'
90
- # error_info = e.backtrace[0]
91
- def find_template_error_line(lines)
92
- lines.select do |line|
93
- line.include?(@path)
94
- end.first
90
+ # Refer to specs and uncomment puts out to see the different types of errors.
91
+ def handle_exception(e)
92
+ # puts "*" * 30
93
+ # puts e.class.to_s.colorize(:cyan)
94
+ # puts e.message.colorize(:cyan)
95
+ # puts e.backtrace
96
+ # puts "*" * 30
97
+ handler = e.is_a?(SyntaxError) ?
98
+ SyntaxErrorHandler.new(@path, e) :
99
+ MainErrorHandler.new(@path, e)
100
+ io = handler.handle
101
+ print_result(io)
95
102
  end
96
103
 
97
-
98
- def pretty_error(e, line)
99
- pretty_path = @path.sub(/^\.\//, '')
100
- io = StringIO.new
101
- io.puts "#{e.class}: #{e.message}"
102
- io.puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{pretty_path}:"
103
-
104
- template = IO.read(@path)
105
- template_lines = template.split("\n")
106
- context = 5 # lines of context
107
- top, bottom = [line-context-1, 0].max, line+context-1
108
- spacing = template_lines.size.to_s.size
109
- template_lines[top..bottom].each_with_index do |line_content, index|
110
- line_number = top+index+1
111
- if line_number == line
112
- io.printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content)
113
- else
114
- io.printf("%#{spacing}d %s\n", line_number, line_content)
115
- end
116
- end
117
-
118
- io.puts backtrace_lines(e)
119
-
104
+ def print_result(io)
120
105
  if ENV['TEST']
121
106
  io.string
122
107
  else
@@ -0,0 +1,75 @@
1
+ class RenderMePretty::Erb
2
+ class BaseHandler
3
+ def initialize(path, exception)
4
+ @path = path
5
+ @exception = exception
6
+ end
7
+
8
+ def pretty_trace(error_line_number, full_message=true)
9
+ io = StringIO.new
10
+
11
+ message = full_message ? ": #{@exception.message}" : ""
12
+ io.puts "#{@exception.class}#{message}".colorize(:red)
13
+
14
+ pretty_path = @path.sub(/^\.\//, '')
15
+ io.puts "Error evaluating ERB template on line #{error_line_number.to_s.colorize(:red)} of: #{pretty_path}:"
16
+
17
+ context = 5 # lines of context
18
+ top, bottom = [error_line_number-context-1, 0].max, error_line_number+context-1
19
+
20
+ lines = IO.read(@path).split("\n")
21
+ spacing = lines.size.to_s.size
22
+ lines[top..bottom].each_with_index do |line_content, index|
23
+ current_line_number = top+index+1
24
+ if current_line_number == error_line_number
25
+ io.printf("%#{spacing}d %s\n".colorize(:red), current_line_number, line_content)
26
+ else
27
+ io.printf("%#{spacing}d %s\n", current_line_number, line_content)
28
+ end
29
+ end
30
+
31
+ io.puts backtrace_lines
32
+ io
33
+ end
34
+
35
+ # Method produces a filtered original stack trace that can be appended to
36
+ # the pretty backtrace output.
37
+ #
38
+ # It parses the original backtrace that looks something like this:
39
+ #
40
+ # (erb):380:in `get_binding'
41
+ # /Users/tung/.rbenv/versions/2.5.0/lib/ruby/2.5.0/erb.rb:885:in `eval'
42
+ # /Users/tung/.rbenv/versions/2.5.0/lib/ruby/2.5.0/erb.rb:885:in `result'
43
+ # /Users/tung/src/tongueroo/lono/vendor/render_me_pretty/lib/render_me_pretty/erb.rb:67:in `render'
44
+ # /Users/tung/src/tongueroo/lono/vendor/render_me_pretty/lib/render_me_pretty.rb:11:in `result'
45
+ # /Users/tung/src/tongueroo/lono/lib/lono/template/template.rb:32:in `build'
46
+ # /Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:82:in `block in build_templates'
47
+ # /Users/tung/src/tongueroo/lono/lib/lono/template/dsl.rb:81:in `each'
48
+ def backtrace_lines
49
+ full = ENV['FULL_BACKTRACE']
50
+ if full
51
+ lines = @exception.backtrace
52
+ else
53
+ lines = @exception.backtrace
54
+ # This filtering business makes is hiding useful info.
55
+ # Think it was needed for ERB but Tilt provides a better stack trace.
56
+ # Commenting out for now.
57
+
58
+ # filter out internal lines
59
+ # removal_index = lines.find_index { |l| l =~ %r[lib/render_me_pretty] }
60
+ # lines = lines[removal_index..-1] # remove leading lines above the lib/
61
+ # render_me_pretty lines by keeping lines past the removal index
62
+ # lines.reject! { |l| l =~ %r[lib/render_me_pretty] } # now filter out
63
+ # render_me_pretty lines
64
+ lines = lines[0..7] # keep 8 lines
65
+ end
66
+ lines[0] = lines[0].colorize(:red)
67
+
68
+ # header
69
+ lines.unshift "\nOriginal backtrace#{full ? '' : ' (last 8 lines)'}:"
70
+ # footer
71
+ lines << "\nRe-run with FULL_BACKTRACE=1 to see all lines"
72
+ lines.join("\n")
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,21 @@
1
+ class RenderMePretty::Erb
2
+ class MainErrorHandler < BaseHandler
3
+ def handle
4
+ line_number = find_line_number
5
+ pretty_trace(line_number, full_message=true) # returns StringIO
6
+ end
7
+
8
+ # For general Tilt errors first line of the backtrace that contains the path
9
+ # of the file we're rendeirng and has the line number. Example:
10
+ #
11
+ # spec/fixtures/invalid.erb:2:in `block in singleton class'
12
+ # error_info = e.backtrace[0]
13
+ def find_line_number
14
+ lines = @exception.backtrace
15
+ error_line = lines.select do |line|
16
+ line.include?(@path)
17
+ end.first
18
+ error_line.split(':')[1].to_i
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ class RenderMePretty::Erb
2
+ class SyntaxErrorHandler < BaseHandler
3
+ def handle
4
+ line_number = find_line_number
5
+ pretty_trace(line_number, full_message=false) # returns StringIO
6
+ end
7
+
8
+ # spec/fixtures/invalid/syntax.erb:2: syntax error, unexpected ';', expecting ']'
9
+ # ); if ENV['TEST' ; _erbout.<<(-" missing ending...
10
+ # ^
11
+ # spec/fixtures/invalid/syntax.erb:12: syntax error, unexpected keyword_end, expecting end-of-input
12
+ # end;end;end;end
13
+ # ^~~
14
+ #
15
+ # We will only find the first line number for the error.
16
+ def find_line_number
17
+ pattern = Regexp.new("#{@path}:(\\\d+): syntax error")
18
+ lines = @exception.message.split("\n")
19
+ found_line = lines.find do |line|
20
+ line.match(pattern)
21
+ end
22
+ md = found_line.match(pattern)
23
+ md[1].to_i # line_number
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderMePretty
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_me_pretty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-16 00:00:00.000000000 Z
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -112,6 +112,9 @@ files:
112
112
  - bin/setup
113
113
  - lib/render_me_pretty.rb
114
114
  - lib/render_me_pretty/erb.rb
115
+ - lib/render_me_pretty/erb/base_handler.rb
116
+ - lib/render_me_pretty/erb/main_error_handler.rb
117
+ - lib/render_me_pretty/erb/syntax_error_handler.rb
115
118
  - lib/render_me_pretty/version.rb
116
119
  - render_me_pretty.gemspec
117
120
  - tilt_examples.rb