From d5a5401f9ae5aacaf41cb34e70c8e9eecf1b2fb9 Mon Sep 17 00:00:00 2001 From: Eliot Horowitz Date: Mon, 28 Dec 2009 17:12:49 -0500 Subject: [PATCH] use code in getLastError SERVER-112 --- buildscripts/errorcodes.py | 3 +-- db/lasterror.cpp | 2 ++ db/lasterror.h | 9 ++++++--- db/pdfile.cpp | 2 +- jstests/error4.js | 7 +++++++ util/assert_util.cpp | 10 +++++----- 6 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 jstests/error4.js diff --git a/buildscripts/errorcodes.py b/buildscripts/errorcodes.py index 16e936e58a1..536a3d2b367 100644 --- a/buildscripts/errorcodes.py +++ b/buildscripts/errorcodes.py @@ -80,6 +80,5 @@ def checkErrorCodes(): if __name__ == "__main__": ok = checkErrorCodes() print( "ok:" + str( ok ) ) - if ok == False: - print( "next: " + str( getNextCode() ) ) + print( "next: " + str( getNextCode() ) ) diff --git a/db/lasterror.cpp b/db/lasterror.cpp index 166bd8f20c7..1503f2864d8 100644 --- a/db/lasterror.cpp +++ b/db/lasterror.cpp @@ -40,6 +40,8 @@ namespace mongo { b.appendNull( "err" ); else b.append( "err", msg ); + if ( code ) + b.append( "code" , code ); if ( updatedExisting != NotUpdate ) b.appendBool( "updatedExisting", updatedExisting == True ); b.append( "n", nObjects ); diff --git a/db/lasterror.h b/db/lasterror.h index 08709335b67..ae9781d9e88 100644 --- a/db/lasterror.h +++ b/db/lasterror.h @@ -26,6 +26,7 @@ namespace mongo { class Message; struct LastError { + int code; string msg; enum UpdatedExistingType { NotUpdate, True, False } updatedExisting; /* todo: nObjects should be 64 bit */ @@ -33,8 +34,9 @@ namespace mongo { int nPrev; bool valid; bool overridenById; - void raiseError(const char *_msg) { + void raiseError(int _code , const char *_msg) { reset( true ); + code = _code; msg = _msg; } void recordUpdate( bool _updatedExisting, int nChanged ) { @@ -51,6 +53,7 @@ namespace mongo { reset(); } void reset( bool _valid = false ) { + code = 0; msg.clear(); updatedExisting = NotUpdate; nObjects = 0; @@ -93,13 +96,13 @@ namespace mongo { map _ids; } lastError; - inline void raiseError(const char *msg) { + inline void raiseError(int code , const char *msg) { LastError *le = lastError.get(); if ( le == 0 ) { DEV log() << "warning: lastError==0 can't report:" << msg << '\n'; return; } - le->raiseError(msg); + le->raiseError(code, msg); } inline void recordUpdate( bool updatedExisting, int nChanged ) { diff --git a/db/pdfile.cpp b/db/pdfile.cpp index cc26c012a50..bba8bb82e68 100644 --- a/db/pdfile.cpp +++ b/db/pdfile.cpp @@ -1532,7 +1532,7 @@ namespace mongo { if( !ok ) { log() << "failed to drop index after a unique key error building it: " << errmsg << ' ' << tabletoidxns << ' ' << name << endl; } - raiseError(saveerrmsg.c_str()); + raiseError(12506,saveerrmsg.c_str()); throw; } } diff --git a/jstests/error4.js b/jstests/error4.js new file mode 100644 index 00000000000..deb2eb2f90a --- /dev/null +++ b/jstests/error4.js @@ -0,0 +1,7 @@ + +t = db.error4; +t.drop() +t.insert( { _id : 1 } ) +t.insert( { _id : 1 } ) +assert.eq( 11000 , db.getLastErrorCmd().code , "A" ) + diff --git a/util/assert_util.cpp b/util/assert_util.cpp index 1c964771ed0..1e17a759138 100644 --- a/util/assert_util.cpp +++ b/util/assert_util.cpp @@ -30,14 +30,14 @@ namespace mongo { void wasserted(const char *msg, const char *file, unsigned line) { problem() << "Assertion failure " << msg << ' ' << file << ' ' << dec << line << endl; sayDbContext(); - raiseError(msg && *msg ? msg : "wassertion failure"); + raiseError(0,msg && *msg ? msg : "wassertion failure"); lastAssert[1].set(msg, getDbContext().c_str(), file, line); } void asserted(const char *msg, const char *file, unsigned line) { problem() << "Assertion failure " << msg << ' ' << file << ' ' << dec << line << endl; sayDbContext(); - raiseError(msg && *msg ? msg : "assertion failure"); + raiseError(0,msg && *msg ? msg : "assertion failure"); lastAssert[0].set(msg, getDbContext().c_str(), file, line); stringstream temp; temp << "assertion " << file << ":" << line; @@ -49,7 +49,7 @@ namespace mongo { void uassert_nothrow(const char *msg) { lastAssert[3].set(msg, getDbContext().c_str(), "", 0); - raiseError(msg); + raiseError(0,msg); } int uacount = 0; @@ -59,14 +59,14 @@ namespace mongo { else RARELY log() << "User Exception " << msg << endl; lastAssert[3].set(msg, getDbContext().c_str(), "", 0); - raiseError(msg); + raiseError(msgid,msg); throw UserException(msgid, msg); } void msgasserted(int msgid, const char *msg) { log() << "Assertion: " << msgid << ":" << msg << endl; lastAssert[2].set(msg, getDbContext().c_str(), "", 0); - raiseError(msg && *msg ? msg : "massert failure"); + raiseError(msgid,msg && *msg ? msg : "massert failure"); breakpoint(); printStackTrace(); // TEMP?? should we get rid of this? TODO throw MsgAssertionException(msgid, msg);