From 3fab8f5fb5de3eb4e98eb1f4f328bab6ad02671d Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 26 Jan 2023 13:14:49 +0000 Subject: [PATCH] - Add DEBUG=1 tip on general exception --- lib/runfile/entrypoint.rb | 6 +++++- lib/runfile/userfile.rb | 4 ++-- spec/approvals/bin/action-error | 2 ++ spec/approvals/bin/syntax-error | 2 ++ spec/runfile/entrypoint_spec.rb | 7 +++++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/runfile/entrypoint.rb b/lib/runfile/entrypoint.rb index 37ce541..a9aac03 100644 --- a/lib/runfile/entrypoint.rb +++ b/lib/runfile/entrypoint.rb @@ -33,11 +33,15 @@ def run! say! 'm`Goodbye`', replace: true 1 rescue => e - puts e.backtrace.reverse if ENV['DEBUG'] + if ENV['DEBUG'] + say! e.backtrace.reverse.join("\n") + say! '---' + end origin = e.backtrace_locations.first location = "#{origin.path}:#{origin.lineno}" say! "rib` #{e.class} ` in nu`#{location}`" say! e.message + say! "\nPrefix with nu`DEBUG=1` for full backtrace" unless ENV['DEBUG'] 1 end diff --git a/lib/runfile/userfile.rb b/lib/runfile/userfile.rb index 2d3f36f..5d538a5 100644 --- a/lib/runfile/userfile.rb +++ b/lib/runfile/userfile.rb @@ -85,14 +85,14 @@ def find_action(args) def run_local(argv) exit_code = nil - + Runner.run docopt, argv: argv, version: version do |args| action = find_action(args) raise ActionNotFound, 'Cannot find action. Is it properly defined?' unless action exit_code = action.run args end - + exit_code if exit_code.is_a? Integer end diff --git a/spec/approvals/bin/action-error b/spec/approvals/bin/action-error index d9b7305..2146a4b 100644 --- a/spec/approvals/bin/action-error +++ b/spec/approvals/bin/action-error @@ -1,2 +1,4 @@  ZeroDivisionError  in runfile:2 divided by 0 + +Prefix with DEBUG=1 for full backtrace diff --git a/spec/approvals/bin/syntax-error b/spec/approvals/bin/syntax-error index 5322085..ed26573 100644 --- a/spec/approvals/bin/syntax-error +++ b/spec/approvals/bin/syntax-error @@ -1,2 +1,4 @@  NameError  in runfile:1 undefined local variable or method `nicely' for # + +Prefix with DEBUG=1 for full backtrace diff --git a/spec/runfile/entrypoint_spec.rb b/spec/runfile/entrypoint_spec.rb index dcc7ead..8ce6570 100644 --- a/spec/runfile/entrypoint_spec.rb +++ b/spec/runfile/entrypoint_spec.rb @@ -35,6 +35,13 @@ expect { subject.run! }.to output(/some error/).to_stderr end + it 'exits displays backtrace on exceptions when DEBUG=1' do + ENV['DEBUG'] = '1' + allow(subject).to receive(:run).and_raise(StandardError, 'some error') + expect { subject.run! }.to output(%r{lib/rspec}).to_stderr + ENV['DEBUG'] = nil + end + context 'when the masterfile action ends with an integer value' do let(:argv) { %w[] }