diff --git a/lib/bundler.rb b/lib/bundler.rb index f4373662ba5..b00887392ab 100644 --- a/lib/bundler.rb +++ b/lib/bundler.rb @@ -120,6 +120,17 @@ def message status_code(23) end + class YAMLSyntaxError < BundlerError + attr_reader :orig_exception + + def initialize(orig_exception, msg) + super(msg) + @orig_exception = orig_exception + end + + status_code(25) + end + class << self attr_writer :bundle_path diff --git a/lib/bundler/friendly_errors.rb b/lib/bundler/friendly_errors.rb index e60f73f7dfe..f6f8fd5623e 100644 --- a/lib/bundler/friendly_errors.rb +++ b/lib/bundler/friendly_errors.rb @@ -5,6 +5,10 @@ module Bundler def self.with_friendly_errors yield + rescue YAMLSyntaxError => e + Bundler.ui.error e.message + Bundler.ui.trace e.orig_exception + exit e.status_code rescue Bundler::Dsl::DSLError => e Bundler.ui.error e.message exit e.status_code diff --git a/lib/bundler/rubygems_integration.rb b/lib/bundler/rubygems_integration.rb index f4c2a12d9d1..ed7d061b9cc 100644 --- a/lib/bundler/rubygems_integration.rb +++ b/lib/bundler/rubygems_integration.rb @@ -67,6 +67,9 @@ def configuration Bundler.ui.error "#{e.class}: #{e.message}" Bundler.ui.trace e raise + rescue Psych::SyntaxError => e + raise YAMLSyntaxError.new(e, "Your RubyGems configuration, which is " \ + "usually located in ~/.gemrc, contains invalid YAML syntax.") end def ruby_engine diff --git a/spec/bundler/friendly_errors_spec.rb b/spec/bundler/friendly_errors_spec.rb index ee0667cb2f5..8e83dc3059c 100644 --- a/spec/bundler/friendly_errors_spec.rb +++ b/spec/bundler/friendly_errors_spec.rb @@ -3,6 +3,33 @@ require "bundler/friendly_errors" describe Bundler, "friendly errors" do + context "with invalid YAML in .gemrc" do + before do + File.open(Gem.configuration.config_file_name, "w") do |f| + f.write "invalid: yaml: hah" + end + end + + after do + FileUtils.rm(Gem.configuration.config_file_name) + end + + it "reports a relevant friendly error message" do + gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + G + + bundle :install, :env => { "DEBUG" => true } + + expect(out).to include("Your RubyGems configuration") + expect(out).to include("invalid YAML syntax") + expect(out).to include("Psych::SyntaxError") + expect(out).not_to include("ERROR REPORT TEMPLATE") + expect(exitstatus).to eq(25) if exitstatus + end + end + it "rescues Thor::AmbiguousTaskError and raises SystemExit" do expect { Bundler.with_friendly_errors do