File tree Expand file tree Collapse file tree 7 files changed +86
-22
lines changed
dummy/app/views/helper_error Expand file tree Collapse file tree 7 files changed +86
-22
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ module WebConsole
7
7
8
8
autoload :View
9
9
autoload :Evaluator
10
+ autoload :ExceptionMapper
10
11
autoload :Session
11
12
autoload :Response
12
13
autoload :Request
Original file line number Diff line number Diff line change
1
+ module WebConsole
2
+ class ExceptionMapper
3
+ def initialize ( exception )
4
+ @backtrace = exception . backtrace
5
+ @bindings = exception . bindings
6
+ end
7
+
8
+ def first
9
+ guess_the_first_application_binding || @bindings . first
10
+ end
11
+
12
+ def []( index )
13
+ guess_binding_for_index ( index ) || @bindings [ index ]
14
+ end
15
+
16
+ private
17
+
18
+ def guess_binding_for_index ( index )
19
+ file , line = @backtrace [ index ] . to_s . split ( ':' )
20
+ line = line . to_i
21
+
22
+ @bindings . find do |binding |
23
+ binding . eval ( '__FILE__' ) == file && binding . eval ( '__LINE__' ) == line
24
+ end
25
+ end
26
+
27
+ def guess_the_first_application_binding
28
+ @bindings . find do |binding |
29
+ binding . eval ( '__FILE__' ) . to_s . start_with? ( Rails . root . to_s )
30
+ end
31
+ end
32
+ end
33
+ end
Original file line number Diff line number Diff line change @@ -30,9 +30,9 @@ def find(id)
30
30
# storage.
31
31
def from ( storage )
32
32
if exc = storage [ :__web_console_exception ]
33
- new ( exc . bindings )
33
+ new ( ExceptionMapper . new ( exc ) )
34
34
elsif binding = storage [ :__web_console_binding ]
35
- new ( binding )
35
+ new ( [ binding ] )
36
36
end
37
37
end
38
38
end
@@ -42,8 +42,8 @@ def from(storage)
42
42
43
43
def initialize ( bindings )
44
44
@id = SecureRandom . hex ( 16 )
45
- @bindings = Array ( bindings )
46
- @evaluator = Evaluator . new ( application_binding || @ bindings. first )
45
+ @bindings = bindings
46
+ @evaluator = Evaluator . new ( bindings . first )
47
47
48
48
store_into_memory
49
49
end
@@ -64,10 +64,6 @@ def switch_binding_to(index)
64
64
65
65
private
66
66
67
- def application_binding
68
- @bindings . find { |b | b . eval ( '__FILE__' ) . to_s . start_with? ( Rails . root . to_s ) }
69
- end
70
-
71
67
def store_into_memory
72
68
inmemory_storage [ id ] = self
73
69
end
Original file line number Diff line number Diff line change 1
1
<% @ok = 42 %>
2
- <% raise %>
2
+ <% params.fetch(:bad_key) %>
Original file line number Diff line number Diff line change
1
+ require 'test_helper'
2
+
3
+ module WebConsole
4
+ class ExcetionMapperTest < ActiveSupport ::TestCase
5
+ test '#first tries to find the first application binding' do
6
+ Rails . stubs ( :root ) . returns Pathname ( __FILE__ ) . parent
7
+
8
+ mapper = ExceptionMapper . new ( External . exception )
9
+
10
+ assert_equal __FILE__ , mapper . first . eval ( '__FILE__' )
11
+ end
12
+
13
+ test '.[] tries match the binding for trace index' do
14
+ exception = External . exception
15
+ mapper = ExceptionMapper . new ( exception )
16
+
17
+ last_index = exception . backtrace . count - 1
18
+ file , line = exception . backtrace . last . split ( ':' )
19
+
20
+ assert_equal file , mapper [ last_index ] . eval ( '__FILE__' )
21
+ assert_equal line . to_i , mapper [ last_index ] . eval ( '__LINE__' )
22
+ end
23
+
24
+ test '.[] fall backs to index if no trace can be found' do
25
+ exception = External . exception
26
+ mapper = ExceptionMapper . new ( exception )
27
+
28
+ unbound_index = exception . backtrace . count
29
+
30
+ assert_nil mapper [ unbound_index ]
31
+ end
32
+ end
33
+ end
Original file line number Diff line number Diff line change @@ -124,7 +124,7 @@ def body
124
124
end
125
125
126
126
test 'can evaluate code and return it as a JSON' do
127
- session , line = Session . new ( binding ) , __LINE__
127
+ session , line = Session . new ( [ binding ] ) , __LINE__
128
128
129
129
Session . stubs ( :from ) . returns ( session )
130
130
@@ -148,7 +148,7 @@ def body
148
148
test 'can be changed mount point' do
149
149
Middleware . mount_point = '/customized/path'
150
150
151
- session , line = Session . new ( binding ) , __LINE__
151
+ session , line = Session . new ( [ binding ] ) , __LINE__
152
152
put "/customized/path/repl_sessions/#{ session . id } " , params : { input : '__LINE__' } , xhr : true
153
153
154
154
assert_equal ( { output : "=> #{ line } \n " } . to_json , response . body )
Original file line number Diff line number Diff line change @@ -17,9 +17,8 @@ def initialize(line)
17
17
end
18
18
19
19
setup do
20
- Rails . stubs ( :root ) . returns Pathname ( __FILE__ ) . parent
21
20
Session . inmemory_storage . clear
22
- @session = Session . new ( binding )
21
+ @session = Session . new ( [ binding ] )
23
22
end
24
23
25
24
test 'returns nil when a session is not found' do
@@ -34,17 +33,19 @@ def initialize(line)
34
33
assert_equal "=> 42\n " , @session . eval ( '40 + 2' )
35
34
end
36
35
37
- test 'find first binding of the rails app' do
38
- session = Session . new ( External . exception . bindings )
39
- assert_equal session . eval ( '__FILE__' ) , "=> \" #{ __FILE__ } \" \n "
40
- end
41
-
42
36
test 'use first binding if no application bindings' do
43
- binding = Object . new
44
- binding . expects ( :eval ) . with ( '__FILE__' ) . returns 'framework'
45
- binding . expects ( :eval ) . with ( 'called?' ) . returns 'yes'
37
+ binding = Object . new . instance_eval do
38
+ def eval ( string )
39
+ case string
40
+ when '__FILE__' then framework
41
+ when 'called?' then 'yes'
42
+ end
43
+ end
44
+
45
+ self
46
+ end
46
47
47
- session = Session . new ( binding )
48
+ session = Session . new ( [ binding ] )
48
49
assert_equal session . eval ( 'called?' ) , "=> \" yes\" \n "
49
50
end
50
51
You can’t perform that action at this time.
0 commit comments