brew/Library/Homebrew/exceptions.rb

625 lines
15 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-07-16 23:17:16 +02:00
require "shellwords"
require "utils"
2018-07-16 23:17:16 +02:00
class UsageError < RuntimeError
attr_reader :reason
def initialize(reason = nil)
@reason = reason
end
def to_s
s = "Invalid usage"
s += ": #{reason}" if reason
s
end
end
class FormulaUnspecifiedError < UsageError
def initialize
super "This command requires a formula argument"
end
end
class KegUnspecifiedError < UsageError
def initialize
super "This command requires a keg argument"
end
end
2011-03-12 23:06:45 -08:00
class MultipleVersionsInstalledError < RuntimeError
attr_reader :name
2010-11-12 20:59:53 -08:00
def initialize(name)
2010-11-12 20:59:53 -08:00
@name = name
super "#{name} has multiple installed versions"
end
end
class NotAKegError < RuntimeError; end
2011-03-12 23:06:45 -08:00
class NoSuchKegError < RuntimeError
attr_reader :name
2010-11-12 20:59:53 -08:00
def initialize(name)
2010-11-12 20:59:53 -08:00
@name = name
super "No such keg: #{HOMEBREW_CELLAR}/#{name}"
end
end
class FormulaValidationError < StandardError
attr_reader :attr, :formula
def initialize(formula, attr, value)
@attr = attr
@formula = formula
super "invalid attribute for formula '#{formula}': #{attr} (#{value.inspect})"
end
end
2013-08-06 21:48:05 -07:00
class FormulaSpecificationError < StandardError; end
class MethodDeprecatedError < StandardError
attr_accessor :issues_url
end
class FormulaUnavailableError < RuntimeError
attr_reader :name
attr_accessor :dependent
def initialize(name)
@name = name
end
def dependent_s
"(dependency of #{dependent})" if dependent && dependent != name
end
def to_s
"No available formula with the name \"#{name}\" #{dependent_s}"
end
end
module FormulaClassUnavailableErrorModule
attr_reader :path
attr_reader :class_name
attr_reader :class_list
def to_s
s = super
s += "\nIn formula file: #{path}"
s += "\nExpected to find class #{class_name}, but #{class_list_s}."
s
end
private
def class_list_s
formula_class_list = class_list.select { |klass| klass < Formula }
if class_list.empty?
"found no classes"
elsif formula_class_list.empty?
"only found: #{format_list(class_list)} (not derived from Formula!)"
else
"only found: #{format_list(formula_class_list)}"
end
end
def format_list(class_list)
2018-09-17 19:44:12 +02:00
class_list.map { |klass| klass.name.split("::").last }.join(", ")
end
end
class FormulaClassUnavailableError < FormulaUnavailableError
include FormulaClassUnavailableErrorModule
def initialize(name, path, class_name, class_list)
@path = path
@class_name = class_name
@class_list = class_list
super name
end
end
module FormulaUnreadableErrorModule
attr_reader :formula_error
def to_s
"#{name}: " + formula_error.to_s
end
end
class FormulaUnreadableError < FormulaUnavailableError
include FormulaUnreadableErrorModule
def initialize(name, error)
super(name)
@formula_error = error
end
end
class TapFormulaUnavailableError < FormulaUnavailableError
attr_reader :tap, :user, :repo
def initialize(tap, name)
@tap = tap
@user = tap.user
@repo = tap.repo
super "#{tap}/#{name}"
end
def to_s
s = super
s += "\nPlease tap it and then try again: brew tap #{tap}" unless tap.installed?
s
end
end
class TapFormulaClassUnavailableError < TapFormulaUnavailableError
include FormulaClassUnavailableErrorModule
attr_reader :tap
def initialize(tap, name, path, class_name, class_list)
@path = path
@class_name = class_name
@class_list = class_list
super tap, name
end
end
class TapFormulaUnreadableError < TapFormulaUnavailableError
include FormulaUnreadableErrorModule
def initialize(tap, name, error)
super(tap, name)
@formula_error = error
end
end
2015-05-08 19:16:06 +08:00
class TapFormulaAmbiguityError < RuntimeError
attr_reader :name, :paths, :formulae
def initialize(name, paths)
2015-05-08 19:16:06 +08:00
@name = name
@paths = paths
@formulae = paths.map do |path|
"#{Tap.from_path(path).name}/#{path.basename(".rb")}"
2015-05-08 19:16:06 +08:00
end
2017-10-15 02:28:32 +02:00
super <<~EOS
2015-05-08 19:16:06 +08:00
Formulae found in multiple taps: #{formulae.map { |f| "\n * #{f}" }.join}
2019-04-08 12:47:15 -04:00
Please use the fully-qualified name (e.g. #{formulae.first}) to refer to the formula.
2015-05-08 19:16:06 +08:00
EOS
end
end
class TapFormulaWithOldnameAmbiguityError < RuntimeError
attr_reader :name, :possible_tap_newname_formulae, :taps
def initialize(name, possible_tap_newname_formulae)
@name = name
@possible_tap_newname_formulae = possible_tap_newname_formulae
@taps = possible_tap_newname_formulae.map do |newname|
newname =~ HOMEBREW_TAP_FORMULA_REGEX
"#{Regexp.last_match(1)}/#{Regexp.last_match(2)}"
end
2017-10-15 02:28:32 +02:00
super <<~EOS
2015-08-22 13:15:33 +08:00
Formulae with '#{name}' old name found in multiple taps: #{taps.map { |t| "\n * #{t}" }.join}
2019-04-08 12:47:15 -04:00
Please use the fully-qualified name (e.g. #{taps.first}/#{name}) to refer to the formula or use its new name.
EOS
end
end
2015-06-13 14:32:10 +08:00
class TapUnavailableError < RuntimeError
attr_reader :name
def initialize(name)
2015-06-13 14:32:10 +08:00
@name = name
2017-10-15 02:28:32 +02:00
super <<~EOS
2015-06-13 14:32:10 +08:00
No available tap #{name}.
EOS
end
end
class TapRemoteMismatchError < RuntimeError
attr_reader :name
attr_reader :expected_remote
attr_reader :actual_remote
def initialize(name, expected_remote, actual_remote)
@name = name
@expected_remote = expected_remote
@actual_remote = actual_remote
2017-10-15 02:28:32 +02:00
super <<~EOS
Tap #{name} remote mismatch.
#{expected_remote} != #{actual_remote}
EOS
end
end
2015-11-07 16:01:05 +08:00
class TapAlreadyTappedError < RuntimeError
attr_reader :name
def initialize(name)
@name = name
2017-10-15 02:28:32 +02:00
super <<~EOS
2015-11-07 16:01:05 +08:00
Tap #{name} already tapped.
EOS
end
end
class TapAlreadyUnshallowError < RuntimeError
attr_reader :name
def initialize(name)
@name = name
2017-10-15 02:28:32 +02:00
super <<~EOS
Tap #{name} already a full clone.
EOS
end
end
2015-08-09 22:42:46 +08:00
class TapPinStatusError < RuntimeError
attr_reader :name, :pinned
2015-08-22 13:15:33 +08:00
def initialize(name, pinned)
2015-08-09 22:42:46 +08:00
@name = name
@pinned = pinned
super pinned ? "#{name} is already pinned." : "#{name} is already unpinned."
end
end
class OperationInProgressError < RuntimeError
def initialize(name)
2017-10-15 02:28:32 +02:00
message = <<~EOS
Operation already in progress for #{name}
Another active Homebrew process is already using #{name}.
Please wait for it to finish or terminate it to continue.
2018-06-06 23:34:19 -04:00
EOS
super message
end
end
2013-08-06 21:48:05 -07:00
class CannotInstallFormulaError < RuntimeError; end
class FormulaInstallationAlreadyAttemptedError < RuntimeError
2014-09-12 21:28:25 -05:00
def initialize(formula)
2015-05-27 22:16:48 +08:00
super "Formula installation already attempted: #{formula.full_name}"
end
end
class UnsatisfiedRequirements < RuntimeError
def initialize(reqs)
if reqs.length == 1
super "An unsatisfied requirement failed this build."
else
super "Unsatisfied requirements failed this build."
end
end
end
class FormulaConflictError < RuntimeError
attr_reader :formula, :conflicts
def initialize(formula, conflicts)
@formula = formula
@conflicts = conflicts
super message
end
def conflict_message(conflict)
message = []
message << " #{conflict.name}"
message << ": because #{conflict.reason}" if conflict.reason
message.join
end
def message
message = []
message << "Cannot install #{formula.full_name} because conflicting formulae are installed."
message.concat conflicts.map { |c| conflict_message(c) } << ""
2017-10-15 02:28:32 +02:00
message << <<~EOS
2017-06-01 16:06:51 +02:00
Please `brew unlink #{conflicts.map(&:name) * " "}` before continuing.
Unlinking removes a formula's symlinks from #{HOMEBREW_PREFIX}. You can
link the formula again after the install finishes. You can --force this
2019-04-08 12:47:15 -04:00
install, but the build may fail or cause obscure side effects in the
resulting software.
2018-06-06 23:34:19 -04:00
EOS
message.join("\n")
end
end
class FormulaAmbiguousPythonError < RuntimeError
def initialize(formula)
2017-10-15 02:28:32 +02:00
super <<~EOS
The version of python to use with the virtualenv in the `#{formula.full_name}` formula
cannot be guessed automatically. If the simultaneous use of python and python@2
is intentional, please add `:using => "python"` or `:using => "python@2"` to
`virtualenv_install_with_resources` to resolve the ambiguity manually.
EOS
end
end
class BuildError < RuntimeError
attr_reader :cmd, :args, :env
attr_accessor :formula, :options
def initialize(formula, cmd, args, env)
@formula = formula
@cmd = cmd
@args = args
@env = env
pretty_args = Array(args).map { |arg| arg.to_s.gsub " ", "\\ " }.join(" ")
super "Failed executing: #{cmd} #{pretty_args}".strip
end
def issues
@issues ||= fetch_issues
end
def fetch_issues
GitHub.issues_for_formula(formula.name, tap: formula.tap)
rescue GitHub::RateLimitExceededError => e
opoo e.message
[]
end
def dump
puts
if ARGV.verbose?
require "system_config"
require "build_environment"
ohai "Formula"
puts "Tap: #{formula.tap}" if formula.tap?
puts "Path: #{formula.path}"
2012-09-27 15:39:16 -04:00
ohai "Configuration"
SystemConfig.dump_verbose_config
2012-09-27 15:39:16 -04:00
ohai "ENV"
Homebrew.dump_build_env(env)
puts
2015-05-27 22:16:48 +08:00
onoe "#{formula.full_name} #{formula.version} did not build"
2015-04-25 22:07:06 -04:00
unless (logs = Dir["#{formula.logs}/*"]).empty?
puts "Logs:"
puts logs.map { |fn| " #{fn}" }.join("\n")
end
2012-09-27 15:39:16 -04:00
end
if formula.tap && defined?(OS::ISSUES_URL)
if formula.tap.official?
puts Formatter.error(Formatter.url(OS::ISSUES_URL), label: "READ THIS")
elsif issues_url = formula.tap.issues_url
2017-10-15 02:28:32 +02:00
puts <<~EOS
If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
2019-04-01 16:02:13 -04:00
#{Formatter.url(issues_url)}
EOS
else
2017-10-15 02:28:32 +02:00
puts <<~EOS
If reporting this issue please do so to (not Homebrew/brew or Homebrew/core):
2019-04-01 16:02:13 -04:00
#{formula.tap}
EOS
end
else
2017-10-15 02:28:32 +02:00
puts <<~EOS
Do not report this issue to Homebrew/brew or Homebrew/core!
EOS
end
2012-08-13 09:50:15 -04:00
puts
if issues.present?
puts "These open issues may also help:"
puts issues.map { |i| "#{i["title"]} #{i["html_url"]}" }.join("\n")
end
require "diagnostic"
checks = Homebrew::Diagnostic::Checks.new
checks.build_error_checks.each do |check|
out = checks.send(check)
next if out.nil?
2018-09-17 02:45:00 +02:00
puts
ofail out
end
end
end
# Raised by {FormulaInstaller#check_dependencies_bottled} and
# {FormulaInstaller#install} if the formula or its dependencies are not bottled
# and are being installed on a system without necessary build tools.
class BuildToolsError < RuntimeError
def initialize(formulae)
2017-10-15 02:28:32 +02:00
super <<~EOS
The following #{"formula".pluralize(formulae.count)}
#{formulae.to_sentence}
cannot be installed as #{"binary package".pluralize(formulae.count)} and must be built from source.
2016-07-16 21:03:36 +01:00
#{DevelopmentTools.installation_instructions}
EOS
end
end
# Raised by Homebrew.install, Homebrew.reinstall, and Homebrew.upgrade
# if the user passes any flags/environment that would case a bottle-only
# installation on a system without build tools to fail.
class BuildFlagsError < RuntimeError
def initialize(flags, bottled: true)
if flags.length > 1
flag_text = "flags"
require_text = "require"
else
flag_text = "flag"
require_text = "requires"
end
message = <<~EOS
The following #{flag_text}:
2015-08-22 13:15:33 +08:00
#{flags.join(", ")}
#{require_text} building tools, but none are installed.
2016-07-16 21:03:36 +01:00
#{DevelopmentTools.installation_instructions}
EOS
message << <<~EOS if bottled
Alternatively, remove the #{flag_text} to attempt bottle installation.
EOS
super message
end
end
# Raised by {CompilerSelector} if the formula fails with all of
# the compilers available on the user's system.
class CompilerSelectionError < RuntimeError
def initialize(formula)
2017-10-15 02:28:32 +02:00
super <<~EOS
2016-07-16 21:03:36 +01:00
#{formula.full_name} cannot be built with any available compilers.
#{DevelopmentTools.custom_installation_instructions}
EOS
end
end
# Raised in {Resource#fetch}.
class DownloadError < RuntimeError
2014-12-29 22:51:55 -05:00
def initialize(resource, cause)
2017-10-15 02:28:32 +02:00
super <<~EOS
Failed to download resource #{resource.download_name.inspect}
2014-12-29 22:51:55 -05:00
#{cause.message}
2018-06-06 23:34:19 -04:00
EOS
2014-12-29 22:51:55 -05:00
set_backtrace(cause.backtrace)
end
end
# Raised in {CurlDownloadStrategy#fetch}.
class CurlDownloadStrategyError < RuntimeError
def initialize(url)
case url
when %r{^file://(.+)}
super "File does not exist: #{Regexp.last_match(1)}"
else
super "Download failed: #{url}"
end
end
end
# Raised by {#safe_system} in `utils.rb`.
class ErrorDuringExecution < RuntimeError
attr_reader :cmd
2018-07-30 10:11:00 +02:00
attr_reader :status
attr_reader :output
2018-07-30 10:11:00 +02:00
def initialize(cmd, status:, output: nil, secrets: [])
@cmd = cmd
2018-07-30 10:11:00 +02:00
@status = status
@output = output
2018-07-30 10:11:00 +02:00
exitstatus = if status.respond_to?(:exitstatus)
status.exitstatus
else
status
end
redacted_cmd = redact_secrets(cmd.shelljoin.gsub('\=', "="), secrets)
s = +"Failure while executing; `#{redacted_cmd}` exited with #{exitstatus}."
2018-07-16 23:17:16 +02:00
unless [*output].empty?
format_output_line = lambda do |type_line|
type, line = *type_line
if type == :stderr
Formatter.error(line)
else
line
end
end
2018-07-16 23:17:16 +02:00
s << " Here's the output:\n"
s << output.map(&format_output_line).join
s << "\n" unless s.end_with?("\n")
2018-07-16 23:17:16 +02:00
end
2019-04-20 14:07:29 +09:00
super s.freeze
end
def stderr
[*output].select { |type,| type == :stderr }.map(&:last).join
end
end
# Raised by {Pathname#verify_checksum} when "expected" is nil or empty.
2013-08-06 21:48:05 -07:00
class ChecksumMissingError < ArgumentError; end
# Raised by {Pathname#verify_checksum} when verification fails.
class ChecksumMismatchError < RuntimeError
attr_reader :expected, :hash_type
def initialize(fn, expected, actual)
@expected = expected
@hash_type = expected.hash_type.to_s.upcase
2017-10-15 02:28:32 +02:00
super <<~EOS
#{@hash_type} mismatch
Expected: #{expected}
2019-04-01 16:02:13 -04:00
Actual: #{actual}
Archive: #{fn}
To retry an incomplete download, remove the file above.
2018-06-06 23:34:19 -04:00
EOS
end
end
class ResourceMissingError < ArgumentError
def initialize(formula, resource)
2015-05-27 22:16:48 +08:00
super "#{formula.full_name} does not define resource #{resource.inspect}"
end
end
class DuplicateResourceError < ArgumentError
def initialize(resource)
super "Resource #{resource.inspect} is defined more than once"
end
end
# Raised when a single patch file is not found and apply hasn't been specified.
class MissingApplyError < RuntimeError; end
class BottleFormulaUnavailableError < RuntimeError
def initialize(bottle_path, formula_path)
2017-10-15 02:28:32 +02:00
super <<~EOS
This bottle does not contain the formula file:
#{bottle_path}
#{formula_path}
EOS
end
end
# Raised when a child process sends us an exception over its error pipe.
class ChildProcessError < RuntimeError
attr_reader :inner
attr_reader :inner_class
def initialize(inner)
@inner = inner
@inner_class = Object.const_get inner["json_class"]
super <<~EOS
2018-10-05 12:04:19 -07:00
An exception occurred within a child process:
#{inner_class}: #{inner["m"]}
EOS
# Clobber our real (but irrelevant) backtrace with that of the inner exception.
set_backtrace inner["b"]
end
end