From cd32dabd529a75f7d8cedf8b56934bcbbfe86509 Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Wed, 7 Apr 2010 14:39:05 -0400 Subject: [PATCH] added GridFileNotFound and GridMD5Failure exceptions --- lib/mongo/exceptions.rb | 6 ++++++ lib/mongo/gridfs/grid_io.rb | 4 ++-- test/grid_file_system_test.rb | 8 +++++++- test/grid_io_test.rb | 13 +++++++++++++ test/grid_test.rb | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/mongo/exceptions.rb b/lib/mongo/exceptions.rb index b3bb1e9..adbc0fc 100644 --- a/lib/mongo/exceptions.rb +++ b/lib/mongo/exceptions.rb @@ -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 diff --git a/lib/mongo/gridfs/grid_io.rb b/lib/mongo/gridfs/grid_io.rb index 187c93f..5fba742 100644 --- a/lib/mongo/gridfs/grid_io.rb +++ b/lib/mongo/gridfs/grid_io.rb @@ -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 diff --git a/test/grid_file_system_test.rb b/test/grid_file_system_test.rb index 6a17bd0..cd30f02 100644 --- a/test/grid_file_system_test.rb +++ b/test/grid_file_system_test.rb @@ -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 diff --git a/test/grid_io_test.rb b/test/grid_io_test.rb index dd8c13c..9e86d86 100644 --- a/test/grid_io_test.rb +++ b/test/grid_io_test.rb @@ -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 diff --git a/test/grid_test.rb b/test/grid_test.rb index 69e4aa6..63432e2 100644 --- a/test/grid_test.rb +++ b/test/grid_test.rb @@ -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})