lib/nanoc/data_sources/filesystem.rb in nanoc-4.4.4 vs lib/nanoc/data_sources/filesystem.rb in nanoc-4.4.5

- old
+ new

@@ -115,11 +115,11 @@ if is_binary && klass == Nanoc::Int::Item meta = (meta_filename && YAML.load_file(meta_filename)) || {} ProtoDocument.new(is_binary: true, filename: content_filename, attributes: meta) elsif is_binary && klass == Nanoc::Int::Layout - raise "The layout file '#{content_filename}' is a binary file, but layouts can only be textual" + raise Errors::BinaryLayout.new(content_filename) else parse_result = parse(content_filename, meta_filename) ProtoDocument.new( is_binary: false, @@ -237,15 +237,15 @@ meta_filenames = filenames.select { |fn| ext_of(fn) == '.yaml' } content_filenames = filenames.select { |fn| ext_of(fn) != '.yaml' } # Check number of files per type unless [0, 1].include?(meta_filenames.size) - raise "Found #{meta_filenames.size} meta files for #{basename}; expected 0 or 1" + raise Errors::MultipleMetaFiles.new(meta_filenames, basename) end unless config[:identifier_type] == 'full' unless [0, 1].include?(content_filenames.size) - raise "Found #{content_filenames.size} content files for #{basename}; expected 0 or 1" + raise Errors::MultipleContentFiles.new(meta_filenames, basename) end end all[basename] = [] all[basename][0] = @@ -350,11 +350,11 @@ return ParseResult.new(content: data, attributes: {}, attributes_data: '') end pieces = data.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3) if pieces.size < 4 - raise "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format." + raise Errors::InvalidFormat.new(content_filename) end meta = parse_metadata(pieces[2], content_filename) content = pieces[4] @@ -364,11 +364,11 @@ # @return [Hash] def parse_metadata(data, filename) begin meta = YAML.load(data) || {} rescue => e - raise "Could not parse YAML for #{filename}: #{e.message}" + raise Errors::UnparseableMetadata.new(filename, e) end verify_meta(meta, filename) meta @@ -384,20 +384,14 @@ @attributes = attributes @attributes_data = attributes_data end end - class InvalidMetadataError < Nanoc::Error - def initialize(filename, klass) - super("The file #{filename} has invalid metadata (expected key-value pairs, found #{klass} instead)") - end - end - def verify_meta(meta, filename) return if meta.is_a?(Hash) - raise InvalidMetadataError.new(filename, meta.class) + raise Errors::InvalidMetadata.new(filename, meta.class) end # Reads the content of the file with the given name and returns a string # in UTF-8 encoding. The original encoding of the string is derived from # the default external encoding, but this can be overridden by the @@ -405,11 +399,11 @@ def read(filename) # Read begin data = File.read(filename) rescue => e - raise "Could not read #{filename}: #{e.inspect}" + raise Errors::FileUnreadable.new(filename, e) end # Fix if data.respond_to?(:encode!) if @config && @config[:encoding] @@ -420,27 +414,23 @@ end begin data.encode!('UTF-8') rescue - raise_encoding_error(filename, original_encoding) + raise Errors::InvalidEncoding.new(filename, original_encoding) end unless data.valid_encoding? - raise_encoding_error(filename, original_encoding) + raise Errors::InvalidEncoding.new(filename, original_encoding) end end # Remove UTF-8 BOM (ugly) data.delete!("\xEF\xBB\xBF") data end - - # Raises an invalid encoding error for the given filename and encoding. - def raise_encoding_error(filename, encoding) - raise "Could not read #{filename} because the file is not valid #{encoding}." - end end end require_relative 'filesystem/tools' +require_relative 'filesystem/errors'