mirror of
https://github.com/mongodb/mongo.git
synced 2024-12-01 09:32:32 +01:00
implement resetError
and make lasterror better
This commit is contained in:
parent
790ef16736
commit
d3aa125d87
@ -87,9 +87,9 @@ namespace mongo {
|
||||
|
||||
BSONObj getlasterrorcmdobj = fromjson("{getlasterror:1}");
|
||||
|
||||
string DBClientWithCommands::getLastError(const char *dbname) {
|
||||
string DBClientWithCommands::getLastError() {
|
||||
BSONObj info;
|
||||
runCommand(dbname, getlasterrorcmdobj, info);
|
||||
runCommand("admin", getlasterrorcmdobj, info);
|
||||
BSONElement e = info["err"];
|
||||
if( e.eoo() ) return "";
|
||||
if( e.type() == Object ) return e.toString();
|
||||
@ -98,9 +98,9 @@ namespace mongo {
|
||||
|
||||
BSONObj getpreverrorcmdobj = fromjson("{getpreverror:1}");
|
||||
|
||||
BSONObj DBClientWithCommands::getPrevError(const char *dbname) {
|
||||
BSONObj DBClientWithCommands::getPrevError() {
|
||||
BSONObj info;
|
||||
runCommand(dbname, getpreverrorcmdobj, info);
|
||||
runCommand("admin", getpreverrorcmdobj, info);
|
||||
return info;
|
||||
}
|
||||
|
||||
|
@ -281,23 +281,29 @@ namespace mongo {
|
||||
/** Get error result from the last operation on this connection.
|
||||
@return error or empty string if no error.
|
||||
*/
|
||||
string getLastError(const char *dbname);
|
||||
string getLastError();
|
||||
|
||||
|
||||
/* Return the last error which has occurred, even if not the very last operation.
|
||||
/** Return the last error which has occurred, even if not the very last operation.
|
||||
|
||||
Returns:
|
||||
{ err : <error message>, nPrev : <how_many_ops_back_occurred>, ok : 1 }
|
||||
@return { err : <error message>, nPrev : <how_many_ops_back_occurred>, ok : 1 }
|
||||
|
||||
result.err will be null if no error has occurred.
|
||||
*/
|
||||
BSONObj getPrevError(const char *dbname);
|
||||
BSONObj getPrevError();
|
||||
|
||||
/** Reset the previous error state for this connection (accessed via getLastError and
|
||||
getPrevError). Useful when performing several operations at once and then checking
|
||||
for an error after attempting all operations.
|
||||
*/
|
||||
bool resetError() { return simpleCommand("admin", 0, "reseterror"); }
|
||||
|
||||
/* Erase / drop an entire database */
|
||||
bool dropDatabase(const char *dbname, BSONObj *info = 0) {
|
||||
return simpleCommand(dbname, info, "dropDatabase");
|
||||
}
|
||||
|
||||
|
||||
/** Delete the specified collection. */
|
||||
bool dropCollection( const string ns ){
|
||||
assert( ns.find( "." ) != string::npos );
|
||||
int pos = ns.find( "." );
|
||||
|
@ -112,11 +112,13 @@ int main() {
|
||||
assert( conn.findOne(ns, "{}")["name"].str() == "sara" );
|
||||
|
||||
assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" );
|
||||
assert( conn.getLastError("test") == "" );
|
||||
assert( conn.getLastError() == "" );
|
||||
|
||||
// nonexistent index test
|
||||
assert( conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")).hasElement("$err") );
|
||||
assert( conn.getLastError("test") == "hint index not found" );
|
||||
assert( conn.getLastError() == "hint index not found" );
|
||||
conn.resetError();
|
||||
assert( conn.getLastError() == "" );
|
||||
|
||||
//existing index
|
||||
assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );
|
||||
|
Loading…
Reference in New Issue
Block a user