diff options
Diffstat (limited to 'lib/rubygems/errors.rb')
| -rw-r--r-- | lib/rubygems/errors.rb | 88 |
1 files changed, 79 insertions, 9 deletions
diff --git a/lib/rubygems/errors.rb b/lib/rubygems/errors.rb index fc9bfbc0dc..4bbc5217e0 100644 --- a/lib/rubygems/errors.rb +++ b/lib/rubygems/errors.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + #-- # This file contains all the various exceptions and other errors that are used # inside of RubyGems. @@ -19,6 +21,80 @@ module Gem attr_accessor :requirement end + ## + # Raised when trying to activate a gem, and that gem does not exist on the + # system. Instead of rescuing from this class, make sure to rescue from the + # superclass Gem::LoadError to catch all types of load errors. + class MissingSpecError < Gem::LoadError + def initialize(name, requirement, extra_message = nil) + @name = name + @requirement = requirement + @extra_message = extra_message + super(message) + end + + def message # :nodoc: + build_message + + "Checked in 'GEM_PATH=#{Gem.path.join(File::PATH_SEPARATOR)}' #{@extra_message}, execute `gem env` for more information" + end + + private + + def build_message + total = Gem::Specification.stubs.size + "Could not find '#{name}' (#{requirement}) among #{total} total gem(s)\n" + end + end + + ## + # Raised when trying to activate a gem, and the gem exists on the system, but + # not the requested version. Instead of rescuing from this class, make sure to + # rescue from the superclass Gem::LoadError to catch all types of load errors. + class MissingSpecVersionError < MissingSpecError + attr_reader :specs + + def initialize(name, requirement, specs) + @specs = specs + super(name, requirement) + end + + private + + def build_message + names = specs.map(&:full_name) + "Could not find '#{name}' (#{requirement}) - did find: [#{names.join ","}]\n" + end + end + + # Raised when there are conflicting gem specs loaded + + class ConflictError < LoadError + ## + # A Hash mapping conflicting specifications to the dependencies that + # caused the conflict + + attr_reader :conflicts + + ## + # The specification that had the conflict + + attr_reader :target + + def initialize(target, conflicts) + @target = target + @conflicts = conflicts + @name = target.name + + reason = conflicts.map do |act, dependencies| + "#{act.full_name} conflicts with #{dependencies.join(", ")}" + end.join ", " + + # TODO: improve message by saying who activated `con` + + super("Unable to activate #{target.full_name}, because #{reason}") + end + end + class ErrorReason; end # Generated when trying to lookup a gem to indicate that the gem @@ -28,7 +104,6 @@ module Gem # in figuring out why a gem couldn't be installed. # class PlatformMismatch < ErrorReason - ## # the name of the gem attr_reader :name @@ -60,11 +135,7 @@ module Gem ## # A wordy description of the error. def wordy - "Found %s (%s), but was for platform%s %s" % - [@name, - @version, - @platforms.size == 1 ? '' : 's', - @platforms.join(' ,')] + format("Found %s (%s), but was for platform%s %s", @name, @version, @platforms.size == 1 ? "" : "s", @platforms.join(" ,")) end end @@ -73,7 +144,6 @@ module Gem # data from a source class SourceFetchProblem < ErrorReason - ## # Creates a new SourceFetchProblem for the given +source+ and +error+. @@ -96,12 +166,12 @@ module Gem # An English description of the error. def wordy - "Unable to download data from #{@source.uri} - #{@error.message}" + "Unable to download data from #{Gem::Uri.redact(@source.uri)} - #{@error.message}" end ## # The "exception" alias allows you to call raise on a SourceFetchProblem. - alias exception error + alias_method :exception, :error end end |
