Skip to content

Commit

Permalink
Special handling of exceptions (prints using full_message)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyObtiva committed Jul 24, 2020
1 parent 63c8203 commit cc4eb76
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- Provide partial support for Opal Ruby (missing display of file name, line number, and source code)
- `source_line_count` option
- `wraper` option for including both `header` and `footer`
- Special handling of exceptions (prints using full_message)

## 0.8.2

Expand Down
4 changes: 0 additions & 4 deletions TODO.md
Expand Up @@ -4,10 +4,6 @@ Here are tasks considered for future versions. Once done, they are moved to the

## Next

### 0.9.0

- Special handling of exceptions (using full_message not to_s)

### Version TBD

- Support pd arg1, arg2, arg3
Expand Down
4 changes: 3 additions & 1 deletion lib/puts_debuggerer.rb
Expand Up @@ -654,7 +654,9 @@ def __build_pd_data__(object, print_engine_options=nil, source_line_count=nil)
pd_expression: __caller_pd_expression__(depth, source_line_count),
object: object,
object_printer: lambda do
if PutsDebuggerer.print_engine.is_a?(Proc)
if object.is_a?(Exception) && object.respond_to?(:full_message)
puts object.full_message
elsif PutsDebuggerer.print_engine.is_a?(Proc)
PutsDebuggerer.print_engine.call(object)
else
if print_engine_options.to_h.empty?
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/puts_debuggerer_spec.rb
Expand Up @@ -12,30 +12,46 @@
PutsDebuggerer.caller = nil
PutsDebuggerer.app_path = nil
end

it 'prints file, line number, ruby expression, and evaluated string object; returns evaluated object' do
name = 'Robert'
expect(PutsDebuggererInvoker.dynamic_greeting(name)).to eq('Hello Robert')
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:10\n > pd \"Hello \#{name}\"\n => \"Hello Robert\"\n")
end

it 'prints file, line number, ruby expression, and evaluated string object without extra parentheses when already surrounded; returns evaluated object' do
name = 'Robert'
expect(PutsDebuggererInvoker.parentheses_dynamic_greeting(name)).to eq("Hello Robert")
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:14\n > pd (\"Hello \#{name}\")\n => \"Hello Robert\"\n")
end

it 'prints file, line number, ruby expression, and evaluated numeric object without quotes; returns evaluated integer' do
expect(PutsDebuggererInvoker.numeric_squaring(3)).to eq(9)
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:18\n > pd n*n\n => 9\n")
end

it 'prints inside pd expression' do
name = 'Robert'
expect(PutsDebuggererInvoker.inside_dynamic_greeting(name)).to eq('Hello Robert')
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:35\n > greeting = \"Hello \#{pd(name)}\"\n => \"Robert\"\n")
end

it 'prints exception stack trace' do
class FakeException < Exception
def full_message
'StackTrace'
end
end
e = FakeException.new
PutsDebuggererInvoker.exception_stack_trace(e)
output = $stdout.string
expect(output).to eq("[PD] /Users/User/code/puts_debuggerer/spec/support/puts_debuggerer_invoker.rb:48\n > pd error\n => StackTrace\n")
end

context 'look into puts debuggerer blog post by tenderlove for other goodies to add'
context 'deadlock detection support'
context 'object allocation support' #might need to note having to load this lib first before others for this to work
Expand Down
4 changes: 4 additions & 0 deletions spec/support/puts_debuggerer_invoker.rb
Expand Up @@ -43,4 +43,8 @@ def self.multi_line_dynamic_greeting_source_line_count(name)
pd "Hello " +
name.to_s, source_line_count: 2
end
# intentional empty line
def self.exception_stack_trace(error)
pd error
end
end

0 comments on commit cc4eb76

Please sign in to comment.