lib/berkshelf/errors.rb in berkshelf-2.0.0.beta vs lib/berkshelf/errors.rb in berkshelf-2.0.0

- old
+ new

@@ -13,122 +13,139 @@ class InternalError < BerkshelfError; status_code(99); end class ArgumentError < InternalError; end class AbstractFunction < InternalError def to_s - "Function must be implemented on includer" + 'Function must be implemented on includer' end end class BerksfileNotFound < BerkshelfError; status_code(100); end class NoVersionForConstraints < BerkshelfError; status_code(101); end class DuplicateLocationDefined < BerkshelfError; status_code(102); end class CookbookNotFound < BerkshelfError; status_code(103); end class GitError < BerkshelfError status_code(104) - attr_reader :stderr + # @param [#to_s] stderr + # the error that came from stderr def initialize(stderr) - @stderr = stderr + @stderr = stderr.to_s end + # A common header for all git errors. The #to_s method should + # use this before outputting any specific errors. + # + # @return [String] + def header + 'An error occurred during Git execution:' + end + def to_s - out = "An error occured during Git execution:\n" - out << stderr.prepend_each("\n", "\t") + [ + header, + "", + " " + @stderr.to_s.split("\n").map(&:strip).join("\n "), + "" + ].join("\n") end end - class PrivateGitRepo < GitError; end - class AmbiguousGitRef < GitError - attr_reader :ref + class AmbiguousGitRef < GitError def initialize(ref) @ref = ref end def to_s - out = "An error occurred during Git execution:\n" - out << "Ambiguous Git ref: #{ref}" + [ + header, + "", + " Ambiguous Git ref: '#{@ref}'", + "", + ].join("\n") end end - class InvalidGitRef < GitError - attr_reader :ref + class InvalidGitRef < GitError def initialize(ref) @ref = ref end def to_s - out = "An error occurred during Git execution:\n" - out << "Invalid Git ref: #{ref}" + [ + header, + "", + " Invalid Git ref: '#{@ref}'", + "", + ].join("\n") end end class DuplicateSourceDefined < BerkshelfError; status_code(105); end class NoSolution < BerkshelfError; status_code(106); end class CookbookSyntaxError < BerkshelfError; status_code(107); end - class BerksConfigNotFound < BerkshelfError; status_code(109); end class InvalidGitURI < BerkshelfError status_code(110) - attr_reader :uri # @param [String] uri def initialize(uri) @uri = uri end def to_s - "'#{uri}' is not a valid Git URI." + "'#{@uri}' is not a valid Git URI" end end class UnknownGitHubProtocol < BerkshelfError status_code(110) - attr_reader :protocol # @param [String] protocol def initialize(protocol) @protocol = protocol end def to_s - "'#{self.protocol}' is not a supported Git protocol for the 'github' location key. Please use 'git' instead." + "'#{@protocol}' is not supported for the 'github' location key - please use 'git' instead" end end class GitNotFound < BerkshelfError status_code(110) def to_s - "Could not find a Git executable in your path. Please add it and try again." + 'Could not find a Git executable in your path - please add it and try again' end end class ConstraintNotSatisfied < BerkshelfError; status_code(111); end class InvalidChefAPILocation < BerkshelfError; status_code(112); end class BerksfileReadError < BerkshelfError + # @param [#status_code] original_error def initialize(original_error) @original_error = original_error end status_code(113) def status_code @original_error.respond_to?(:status_code) ? @original_error.status_code : 113 end + + def to_s + [ + "An error occurred while reading the Berksfile:", + "", + " " + @original_error.to_s.split("\n").map(&:strip).join("\n "), + ].join("\n") + end end - # @author Seth Vargo <sethvargo@gmail.com> class MismatchedCookbookName < BerkshelfError status_code(114) - # @return [Berkshelf::Location] - attr_reader :location - - # @return [Berkshelf::CachedCookbook] - attr_reader :cached_cookbook - # @param [Berkshelf::Location] location # the location that is mismatched # @param [Berkshelf::CachedCookbook] cached_cookbook # the cached_cookbook that is mismatched def initialize(location, cached_cookbook) @@ -138,17 +155,17 @@ def to_s [ "In your Berksfile, you have:", "", - " cookbook '#{location.name}'", + " cookbook '#{@location.name}'", "", - "But that cookbook is actually named '#{cached_cookbook.cookbook_name}'.", + "But that cookbook is actually named '#{@cached_cookbook.cookbook_name}'", "", - "This can cause potentially unwanted side-effects in the future.", + "This can cause potentially unwanted side-effects in the future", "", - "NOTE: If you don't explicitly set the `name` attribute in the metadata, the name of the directory will be used!" + "NOTE: If you don't explicitly set the `name` attribute in the metadata, the name of the directory will be used!", ].join("\n") end end class InvalidConfiguration < BerkshelfError @@ -157,19 +174,14 @@ def initialize(errors) @errors = errors end def to_s - strings = ["Invalid configuration:"] - - @errors.each do |key, errors| - errors.each do |error| - strings << " #{key} #{error}" - end - end - - strings.join "\n" + [ + 'Invalid configuration:', + @errors.map { |key, errors| errors.map { |error| " #{key} #{error}" } }, + ].join("\n") end end class ConfigExists < BerkshelfError; status_code(116); end class ConfigurationError < BerkshelfError; status_code(117); end @@ -198,11 +210,11 @@ " #{@cached_cookbook.cookbook_name} (#{@cached_cookbook.version})", "", "does not satisfy the version constraint:", " #{@cached_cookbook.cookbook_name} (#{@location.version_constraint})", "", - "This occurs when the Chef Server has a cookbook with a missing/mis-matched version number in its `metadata.rb`." + "This occurs when the Chef Server has a cookbook with a missing/mis-matched version number in its `metadata.rb`", ].join("\n") end end class ClientKeyFileNotFound < BerkshelfError; status_code(125); end @@ -217,37 +229,38 @@ def initialize(shortname) @shortname = shortname end def to_s - "Unknown site shortname: #{@shortname.inspect}. Supported shortnames are: #{SiteLocation::SHORTNAMES.keys.map(&:inspect).join(',')}" + [ + "Unknown site shortname '#{@shortname}' - supported shortnames are:", + "", + " * " + SiteLocation::SHORTNAMES.keys.join("\n * "), + ].join("\n") end end class OutdatedCookbookSource < BerkshelfError status_code(128) - # @return [Berkshelf::CookbookSource] - attr_reader :locked_source, :source - # @param [Berkshelf::CookbookSource] source # the cookbook source that is outdated def initialize(locked_source, source) @locked_source = locked_source @source = source end def to_s [ - "Berkshelf could not find compatible versions for cookbook '#{source.name}':", + "Berkshelf could not find compatible versions for cookbook '#{@source.name}':", " In Berksfile:", - " #{locked_source.name} (#{locked_source.locked_version})", + " #{@locked_source.name} (#{@locked_source.locked_version})", "", " In Berksfile.lock:", - " #{source.name} (#{source.version_constraint})", + " #{@source.name} (#{@source.version_constraint})", "", - "Try running `berks update #{source.name}, which will try to find '#{source.name}' matching '#{source.version_constraint}'." + "Try running `berks update #{@source.name}, which will try to find '#{@source.name}' matching '#{@source.version_constraint}'.", ].join("\n") end end class EnvironmentNotFound < BerkshelfError @@ -256,31 +269,93 @@ def initialize(environment_name) @environment_name = environment_name end def to_s - %Q[The environment "#{@environment_name}" does not exist.] + "The environment '#{@environment_name}' does not exist" end end class ChefConnectionError < BerkshelfError status_code(130) def to_s - "There was an error connecting to the chef server." + 'There was an error connecting to the Chef Server' end end - # @author Seth Vargo <sethvargo@gmail.com> class UnknownCompressionType < BerkshelfError status_code(131) def initialize(destination) @destination = destination end def to_s - "The file at '#{@destination}' is not a known compression type!" + "The file at '#{@destination}' is not a known compression type" end + end + # Raised when a cookbook or its recipes contain a space or invalid + # character in the path. + # + # @param [Berkshelf::CachedCookbook] cookbook + # the cookbook that failed validation + # @param [Array<#to_s>] files + # the list of files that were not valid + class InvalidCookbookFiles < BerkshelfError + status_code(132) + + def initialize(cookbook, files) + @cookbook = cookbook + @files = files + end + + def to_s + [ + "The cookbook '#{@cookbook.cookbook_name}' has invalid filenames:", + "", + " " + @files.map(&:to_s).join("\n "), + "", + "Please note, spaces are not a valid character in filenames", + ].join("\n") + end + end + + # Raised when a CachedCookbook has a license file that isn't allowed + # by the Berksfile. + # + # @param [Berkshelf::CachedCookbook] cookbook + # the cookbook that failed license validation + class LicenseNotAllowed < BerkshelfError + status_code(133) + + def initialize(cookbook) + @cookbook = cookbook + end + + def to_s + msg = "'#{@cookbook.cookbook_name}' has a license of '#{@cookbook.metadata.license}', but" + msg << " '#{@cookbook.metadata.license}' is not in your list of allowed licenses" + msg + end + end + + # Raised when a cookbook or its recipes contain a space or invalid + # character in the path. + class ConfigNotFound < BerkshelfError + status_code(133) + + # @param [String] type + # the type of config that was not found (Berkshelf, Chef, etc) + # @param [#to_s] path + # the path to the specified Chef config that did not exist + def initialize(type, path) + @type = type.to_s + @path = path + end + + def to_s + "No #{@type.capitalize} config file found at: '#{@path}'!" + end end end