Skip to content

Commit

Permalink
Merge pull request #4063 from rubygems/friendly_errors
Browse files Browse the repository at this point in the history
Raise consistent errors with or without `bundle exec`

(cherry picked from commit fbce3ba)
  • Loading branch information
deivid-rodriguez committed Dec 7, 2020
1 parent a7a8b51 commit a372c7d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 3 additions & 3 deletions bundler/lib/bundler/cli/exec.rb
Expand Up @@ -63,10 +63,10 @@ def kernel_load(file, *args)
Kernel.load(file)
rescue SystemExit, SignalException
raise
rescue Exception => e # rubocop:disable Lint/RescueException
rescue Exception # rubocop:disable Lint/RescueException
Bundler.ui.error "bundler: failed to load command: #{cmd} (#{file})"
backtrace = e.backtrace ? e.backtrace.take_while {|bt| !bt.start_with?(__FILE__) } : []
abort "#{e.class}: #{e.message}\n #{backtrace.join("\n ")}"
Bundler::FriendlyErrors.disable!
raise
end

def process_title(file, args)
Expand Down
15 changes: 15 additions & 0 deletions bundler/lib/bundler/friendly_errors.rb
Expand Up @@ -6,6 +6,18 @@ module Bundler
module FriendlyErrors
module_function

def enable!
@disabled = false
end

def disabled?
@disabled
end

def disable!
@disabled = true
end

def log_error(error)
case error
when YamlSyntaxError
Expand Down Expand Up @@ -114,10 +126,13 @@ def issues_url(exception)
end

def self.with_friendly_errors
FriendlyErrors.enable!
yield
rescue SignalException
raise
rescue Exception => e # rubocop:disable Lint/RescueException
raise if FriendlyErrors.disabled?

FriendlyErrors.log_error(e)
exit FriendlyErrors.exit_status(e)
end
Expand Down
14 changes: 11 additions & 3 deletions bundler/spec/commands/exec_spec.rb
Expand Up @@ -698,15 +698,23 @@ def bin_path(a,b,c)
let(:exit_code) { 1 }
let(:expected_err) do
"bundler: failed to load command: #{path} (#{path})" \
"\nRuntimeError: ERROR\n #{path}:10:in `<top (required)>'"
"\n#{path}:10:in `<top (required)>': ERROR (RuntimeError)"
end

it "runs like a normally executed executable" do
skip "https://github.com/rubygems/rubygems/issues/3351" if Gem.win_platform?

subject
expect(exitstatus).to eq(exit_code)
expect(err).to start_with(expected_err)
expect(out).to eq(expected)
end
it_behaves_like "it runs"
end

context "the executable raises an error without a backtrace" do
let(:executable) { super() << "\nclass Err < Exception\ndef backtrace; end;\nend\nraise Err" }
let(:exit_code) { 1 }
let(:expected_err) { "bundler: failed to load command: #{path} (#{path})\nErr: Err" }
let(:expected_err) { "bundler: failed to load command: #{path} (#{path})\n#{system_gem_path("bin/bundle")}: Err (Err)" }
let(:expected) { super() }

it_behaves_like "it runs"
Expand Down

0 comments on commit a372c7d

Please sign in to comment.