Skip to content

Commit

Permalink
Merge pull request #139 from sdsykes/fix/NoMethodError
Browse files Browse the repository at this point in the history
Handle nil being passed as uri
  • Loading branch information
SamSaffron authored Apr 11, 2023
2 parents f8d0b37 + e5266ff commit b65d75c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/fastimage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class SizeNotFound < FastImageException # :nodoc:
end
class CannotParseImage < FastImageException # :nodoc:
end
class BadImageURI < FastImageException # :nodoc:
end

DefaultTimeout = 2 unless const_defined?(:DefaultTimeout)

Expand Down Expand Up @@ -228,6 +230,8 @@ def initialize(uri, options={})
:size
end

raise BadImageURI if uri.nil?

@type, @state = nil

if uri.respond_to?(:read)
Expand All @@ -254,7 +258,7 @@ def initialize(uri, options={})
Errno::ENETUNREACH, ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT,
OpenSSL::SSL::SSLError
raise ImageFetchFailure if @options[:raise_on_failure]
rescue UnknownImageType
rescue UnknownImageType, BadImageURI
raise if @options[:raise_on_failure]
rescue CannotParseImage
if @options[:raise_on_failure]
Expand Down
10 changes: 10 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,14 @@ def test_canon_raw_formats_are_not_recognised_as_tiff
FastImage.size(TestUrl + "a.CRW", :raise_on_failure=>true)
end
end

def test_returns_nil_when_uri_is_nil
assert_equal nil, FastImage.size(nil)
end

def test_raises_when_uri_is_nil_and_raise_on_failure_is_set
assert_raises(FastImage::BadImageURI) do
FastImage.size(nil, :raise_on_failure => true)
end
end
end

0 comments on commit b65d75c

Please sign in to comment.