Define the EntrySizeError message within the error class.

This commit is contained in:
Robert Haines 2022-08-15 22:02:33 +01:00
parent 7097492dc8
commit 07eca2bae8
3 changed files with 19 additions and 6 deletions

View File

@ -711,10 +711,10 @@ module Zip
bytes_written += buf.bytesize
next unless bytes_written > size && !warned
message = "entry '#{name}' should be #{size}B, but is larger when inflated."
raise ::Zip::EntrySizeError, message if ::Zip.validate_entry_sizes
error = ::Zip::EntrySizeError.new(self)
raise error if ::Zip.validate_entry_sizes
warn "WARNING: #{message}"
warn "WARNING: #{error.message}"
warned = true
end
end

View File

@ -4,7 +4,6 @@ module Zip
class Error < StandardError; end
class DestinationFileExistsError < Error; end
class EntryNameError < Error; end
class EntrySizeError < Error; end
class CompressionMethodError < Error
attr_reader :compression_method
@ -44,6 +43,19 @@ module Zip
end
end
class EntrySizeError < Error
attr_reader :entry
def initialize(entry)
super()
@entry = entry
end
def message
"Entry '#{@entry.name}' should be #{@entry.size}B, but is larger when inflated."
end
end
class SplitArchiveError < Error
def message
'Rubyzip cannot extract from split archives at this time.'

View File

@ -139,9 +139,10 @@ class ZipFileExtractTest < MiniTest::Test
error = assert_raises ::Zip::EntrySizeError do
a_entry.extract
end
assert_equal \
"entry 'a' should be 1B, but is larger when inflated.",
assert_equal(
"Entry 'a' should be 1B, but is larger when inflated.",
error.message
)
end
end
end