Added error? and error() methods to DB.

This commit is contained in:
Jim Menard 2009-01-23 13:47:27 -05:00
parent 3ffc8942b0
commit f9c9eefdb4
2 changed files with 34 additions and 0 deletions

View File

@ -242,6 +242,28 @@ module XGen
ok?(db_command(:drop => name))
end
# Returns the error message from the most recently executed database
# operation for this connection, or +nil+ if there was no error.
#
# Note: as of this writing, errors are only detected on the db server
# for certain kinds of operations (writes). The plan is to change this
# so that all operations will set the error if needed.
def error
doc = db_command(:getlasterror => 1)
raise "error retrieving last error: #{doc}" unless ok?(doc)
doc['err']
end
# Returns +true+ if there is was an error caused by the most recently
# executed database operation.
#
# Note: as of this writing, errors are only detected on the db server
# for certain kinds of operations (writes). The plan is to change this
# so that all operations will set the error if needed.
def error?
error != nil
end
# Returns true if this database is a master (or is not paired with any
# other database), false if it is a slave.
def master?

View File

@ -106,4 +106,16 @@ class DBTest < Test::Unit::TestCase
assert db.connected?
end
def test_error
doc = @db.send(:db_command, :forceerror => 1)
assert @db.error?
err = @db.error
assert_match /forced error/, err
# ask again
assert @db.error?
err2 = @db.error
assert_equal err, err2
end
end