brew/Library/Homebrew/cask/exceptions.rb

240 lines
5.9 KiB
Ruby
Raw Permalink Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2018-09-06 08:29:14 +02:00
module Cask
2020-08-24 23:31:55 +02:00
# General cask error.
2016-09-24 13:52:43 +02:00
class CaskError < RuntimeError; end
2016-08-18 22:11:42 +03:00
2020-08-24 23:31:55 +02:00
# Cask error containing multiple other errors.
class MultipleCaskErrors < CaskError
def initialize(errors)
super()
2020-08-19 17:12:32 +01:00
@errors = errors
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def to_s
<<~EOS
Problems with multiple casks:
#{@errors.map(&:to_s).join("\n")}
EOS
end
end
2020-08-24 23:31:55 +02:00
# Abstract cask error containing a cask token.
2016-09-24 13:52:43 +02:00
class AbstractCaskErrorWithToken < CaskError
2020-10-20 12:03:48 +02:00
sig { returns(String) }
attr_reader :token
sig { returns(String) }
attr_reader :reason
2016-08-18 22:11:42 +03:00
2017-06-11 02:00:59 +02:00
def initialize(token, reason = nil)
2020-08-19 17:12:32 +01:00
super()
2020-10-20 12:03:48 +02:00
@token = token.to_s
2017-06-11 02:00:59 +02:00
@reason = reason.to_s
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error when a cask is not installed.
2016-09-24 13:52:43 +02:00
class CaskNotInstalledError < AbstractCaskErrorWithToken
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def to_s
2017-06-11 02:00:59 +02:00
"Cask '#{token}' is not installed."
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
# Error when a cask cannot be installed.
class CaskCannotBeInstalledError < AbstractCaskErrorWithToken
attr_reader :message
def initialize(token, message)
super(token)
@message = message
end
sig { returns(String) }
def to_s
"Cask '#{token}' has been #{message}"
end
end
2020-08-24 23:31:55 +02:00
# Error when a cask conflicts with another cask.
2017-08-05 15:56:34 +02:00
class CaskConflictError < AbstractCaskErrorWithToken
attr_reader :conflicting_cask
def initialize(token, conflicting_cask)
super(token)
@conflicting_cask = conflicting_cask
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2017-08-05 15:56:34 +02:00
def to_s
"Cask '#{token}' conflicts with '#{conflicting_cask}'."
end
end
2020-08-24 23:31:55 +02:00
# Error when a cask is not available.
2016-09-24 13:52:43 +02:00
class CaskUnavailableError < AbstractCaskErrorWithToken
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def to_s
"Cask '#{token}' is unavailable#{reason.empty? ? "." : ": #{reason}"}"
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error when a cask is unreadable.
class CaskUnreadableError < CaskUnavailableError
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def to_s
"Cask '#{token}' is unreadable#{reason.empty? ? "." : ": #{reason}"}"
end
end
# Error when a cask in a specific tap is not available.
class TapCaskUnavailableError < CaskUnavailableError
attr_reader :tap
def initialize(tap, token)
super("#{tap}/#{token}")
@tap = tap
end
sig { returns(String) }
def to_s
s = super
s += "\nPlease tap it and then try again: brew tap #{tap}" unless tap.installed?
s
end
end
2022-06-17 15:22:41 -04:00
# Error when a cask with the same name is found in multiple taps.
class TapCaskAmbiguityError < CaskError
2024-02-20 18:08:55 +01:00
sig { returns(String) }
attr_reader :token
sig { returns(T::Array[CaskLoader::FromNameLoader]) }
attr_reader :loaders
sig { params(token: String, loaders: T::Array[CaskLoader::FromNameLoader]).void }
def initialize(token, loaders)
@loaders = loaders
taps = loaders.map(&:tap)
casks = taps.map { |tap| "#{tap}/#{token}" }
cask_list = casks.sort.map { |f| "\n * #{f}" }.join
2022-06-17 15:22:41 -04:00
super <<~EOS
Cask #{token} exists in multiple taps:#{cask_list}
Please use the fully-qualified name (e.g. #{casks.first}) to refer to a specific Cask.
2022-06-17 15:22:41 -04:00
EOS
end
end
2020-08-24 23:31:55 +02:00
# Error when a cask already exists.
2016-09-24 13:52:43 +02:00
class CaskAlreadyCreatedError < AbstractCaskErrorWithToken
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def to_s
2020-11-18 08:10:21 +01:00
%Q(Cask '#{token}' already exists. Run #{Formatter.identifier("brew edit --cask #{token}")} to edit it.)
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error when there is a cyclic cask dependency.
2017-06-28 17:53:59 +02:00
class CaskCyclicDependencyError < AbstractCaskErrorWithToken
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def to_s
"Cask '#{token}' includes cyclic dependencies on other Casks#{reason.empty? ? "." : ": #{reason}"}"
2017-06-28 17:53:59 +02:00
end
end
2020-08-24 23:31:55 +02:00
# Error when a cask depends on itself.
2017-06-28 17:53:59 +02:00
class CaskSelfReferencingDependencyError < CaskCyclicDependencyError
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2017-06-28 17:53:59 +02:00
def to_s
"Cask '#{token}' depends on itself."
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error when no cask is specified.
2016-09-24 13:52:43 +02:00
class CaskUnspecifiedError < CaskError
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def to_s
2017-06-11 02:00:59 +02:00
"This command requires a Cask token."
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error when a cask is invalid.
2016-09-24 13:52:43 +02:00
class CaskInvalidError < AbstractCaskErrorWithToken
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-09-24 13:52:43 +02:00
def to_s
"Cask '#{token}' definition is invalid#{reason.empty? ? "." : ": #{reason}"}"
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error when a cask token does not match the file name.
2017-06-11 02:00:59 +02:00
class CaskTokenMismatchError < CaskInvalidError
2016-09-24 13:52:43 +02:00
def initialize(token, header_token)
2017-06-11 02:00:59 +02:00
super(token, "Token '#{header_token}' in header line does not match the file name.")
2016-09-24 13:52:43 +02:00
end
2016-08-18 22:11:42 +03:00
end
2020-08-24 23:31:55 +02:00
# Error during quarantining of a file.
class CaskQuarantineError < CaskError
attr_reader :path, :reason
def initialize(path, reason)
super()
2020-08-19 17:12:32 +01:00
@path = path
@reason = reason
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def to_s
s = "Failed to quarantine #{path}."
unless reason.empty?
s << " Here's the reason:\n"
s << Formatter.error(reason)
s << "\n" unless reason.end_with?("\n")
end
s.freeze
end
end
2020-08-24 23:31:55 +02:00
# Error while propagating quarantine information to subdirectories.
class CaskQuarantinePropagationError < CaskQuarantineError
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def to_s
s = "Failed to quarantine one or more files within #{path}."
unless reason.empty?
s << " Here's the reason:\n"
s << Formatter.error(reason)
s << "\n" unless reason.end_with?("\n")
end
s.freeze
end
end
2020-08-24 23:31:55 +02:00
# Error while removing quarantine information.
class CaskQuarantineReleaseError < CaskQuarantineError
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def to_s
s = "Failed to release #{path} from quarantine."
unless reason.empty?
s << " Here's the reason:\n"
s << Formatter.error(reason)
s << "\n" unless reason.end_with?("\n")
end
s.freeze
end
end
2016-08-18 22:11:42 +03:00
end