added GridFileNotFound and GridMD5Failure exceptions

This commit is contained in:
Kyle Banker 2010-04-07 14:39:05 -04:00
parent 3133380341
commit cd32dabd52
5 changed files with 29 additions and 4 deletions

View File

@ -27,6 +27,12 @@ module Mongo
# Raised on fatal errors to GridFS.
class GridError < MongoRubyError; end
# Raised on fatal errors to GridFS.
class GridFileNotFound < GridError; end
# Raised on fatal errors to GridFS.
class GridMD5Failure < GridError; end
# Raised when invalid arguments are sent to Mongo Ruby methods.
class MongoArgumentError < MongoRubyError; end

View File

@ -287,7 +287,7 @@ module Mongo
# Initialize the class for reading a file.
def init_read
doc = @files.find(@query, @query_opts).next_document
raise GridError, "Could not open file matching #{@query.inspect} #{@query_opts.inspect}" unless doc
raise GridFileNotFound, "Could not open file matching #{@query.inspect} #{@query_opts.inspect}" unless doc
@files_id = doc['_id']
@content_type = doc['contentType']
@ -342,7 +342,7 @@ module Mongo
if @safe
@client_md5 = @local_md5.hexdigest
if @local_md5 != @server_md5
raise GridError, "File on server failed MD5 check"
raise GridMD5Failure, "File on server failed MD5 check"
end
else
@server_md5

View File

@ -75,9 +75,15 @@ class GridFileSystemTest < Test::Unit::TestCase
assert_equal data.length, @data.length
end
should "raise exception if file not found" do
assert_raise GridFileNotFound do
@grid.open('io', 'r') { |f| f.write('hello') }
end
end
should "raise exception if not opened for write" do
assert_raise GridError do
@grid.open('io', 'r') { |f| f.write('hello') }
@grid.open('sample', 'r') { |f| f.write('hello') }
end
end

View File

@ -49,6 +49,19 @@ class GridIOTest < Test::Unit::TestCase
assert file.close
assert_equal file.server_md5, file.client_md5
end
should "raise an exception when check fails" do
io = File.open(File.join(File.dirname(__FILE__), 'data', 'sample_file.pdf'), 'r')
db = mock()
db.stubs(:command).returns({'md5' => '12345'})
@files.expects(:db).returns(db)
file = GridIO.new(@files, @chunks, 'bigfile', 'w', :safe => true)
file.write(io)
assert_raise GridMD5Failure do
assert file.close
end
assert_not_equal file.server_md5, file.client_md5
end
end
context "Content types" do

View File

@ -39,7 +39,7 @@ class GridTest < Test::Unit::TestCase
should "delete the file and any chunks" do
@grid.delete(@id)
assert_raise GridError do
assert_raise GridFileNotFound do
@grid.get(@id)
end
assert_equal nil, @db['test-fs']['chunks'].find_one({:files_id => @id})