From 2c09ff42b2e96f28dda283730bdb9deb68946843 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 15 Oct 2008 16:30:15 -0700 Subject: [PATCH] Raise MogileFS::ReadOnly error for readonly instances Also easier to trap and deal with --- lib/mogilefs.rb | 5 +++++ lib/mogilefs/admin.rb | 10 +++++----- lib/mogilefs/mogilefs.rb | 10 +++++----- test/test_mogilefs.rb | 12 ++++++------ 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/mogilefs.rb b/lib/mogilefs.rb index 8f499e5..0d37d34 100644 --- a/lib/mogilefs.rb +++ b/lib/mogilefs.rb @@ -13,6 +13,11 @@ module MogileFS class Error < StandardError; end class UnreadableSocketError < Error; end + class ReadOnlyError < Error + def message + 'readonly mogilefs' + end + end end diff --git a/lib/mogilefs/admin.rb b/lib/mogilefs/admin.rb index c03b394..d3774b7 100644 --- a/lib/mogilefs/admin.rb +++ b/lib/mogilefs/admin.rb @@ -155,7 +155,7 @@ class MogileFS::Admin < MogileFS::Client # Creates a new domain named +domain+. Returns nil if creation failed. def create_domain(domain) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? res = @backend.create_domain :domain => domain return res['domain'] unless res.nil? end @@ -164,7 +164,7 @@ class MogileFS::Admin < MogileFS::Client # Deletes +domain+. Returns true if successful, false if not. def delete_domain(domain) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? res = @backend.delete_domain :domain => domain return !res.nil? end @@ -216,7 +216,7 @@ class MogileFS::Admin < MogileFS::Client # Deletes host +host+. Returns nil on failure. def delete_host(host) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? res = @backend.delete_host :host => host return !res.nil? end @@ -226,7 +226,7 @@ class MogileFS::Admin < MogileFS::Client # 'alive', 'down', or 'dead'. def change_device_state(host, device, state) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? res = @backend.set_state :host => host, :device => device, :state => state return !res.nil? end @@ -238,7 +238,7 @@ class MogileFS::Admin < MogileFS::Client # +action+. Returns the class name if successful, nil if not. def modify_class(domain, klass, mindevcount, action) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? res = @backend.send("#{action}_class", :domain => domain, :class => klass, :mindevcount => mindevcount) diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb index 848061e..0a62dba 100644 --- a/lib/mogilefs/mogilefs.rb +++ b/lib/mogilefs/mogilefs.rb @@ -121,7 +121,7 @@ class MogileFS::MogileFS < MogileFS::Client # The +block+ operates like File.open. def new_file(key, klass, bytes = 0, &block) # :yields: file - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? res = @backend.create_open(:domain => @domain, :class => klass, :key => key, :multi_dest => 1) @@ -161,7 +161,7 @@ class MogileFS::MogileFS < MogileFS::Client # either a file name or an object that responds to #read. def store_file(key, klass, file) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? new_file key, klass do |mfp| if file.respond_to? :sysread then @@ -181,7 +181,7 @@ class MogileFS::MogileFS < MogileFS::Client # Stores +content+ into +key+ in class +klass+. def store_content(key, klass, content) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? new_file key, klass do |mfp| mfp << content @@ -194,7 +194,7 @@ class MogileFS::MogileFS < MogileFS::Client # Removes +key+. def delete(key) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? @backend.delete :domain => @domain, :key => key end @@ -210,7 +210,7 @@ class MogileFS::MogileFS < MogileFS::Client # Renames a key +from+ to key +to+. def rename(from, to) - raise 'readonly mogilefs' if readonly? + raise MogileFS::ReadOnlyError if readonly? @backend.rename :domain => @domain, :from_key => from, :to_key => to nil diff --git a/test/test_mogilefs.rb b/test/test_mogilefs.rb index 96d7532..6ac9d55 100644 --- a/test/test_mogilefs.rb +++ b/test/test_mogilefs.rb @@ -104,7 +104,7 @@ class TestMogileFS__MogileFS < TestMogileFS def test_delete_readonly @client.readonly = true - assert_raises RuntimeError do + assert_raises MogileFS::ReadOnlyError do @client.delete 'no_such_key' end end @@ -134,14 +134,14 @@ class TestMogileFS__MogileFS < TestMogileFS def test_new_file_http @client.readonly = true - assert_raises RuntimeError do + assert_raises MogileFS::ReadOnlyError do @client.new_file 'new_key', 'test' end end def test_new_file_readonly @client.readonly = true - assert_raises RuntimeError do + assert_raises MogileFS::ReadOnlyError do @client.new_file 'new_key', 'test' end end @@ -260,14 +260,14 @@ Content-Length: 0\r def test_store_content_readonly @client.readonly = true - assert_raises RuntimeError do + assert_raises MogileFS::ReadOnlyError do @client.store_content 'new_key', 'test', nil end end def test_store_file_readonly @client.readonly = true - assert_raises RuntimeError do + assert_raises MogileFS::ReadOnlyError do @client.store_file 'new_key', 'test', nil end end @@ -299,7 +299,7 @@ Content-Length: 0\r def test_rename_readonly @client.readonly = true - e = assert_raises RuntimeError do + e = assert_raises MogileFS::ReadOnlyError do @client.rename 'new_key', 'test' end -- 2.11.4.GIT