Files
mongo/db/lasterror.cpp

143 lines
3.8 KiB
C++
Raw Normal View History

// lasterror.cpp
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2010-04-27 15:27:52 -04:00
#include "pch.h"
#include "../util/unittest.h"
#include "../util/net/message.h"
#include "lasterror.h"
#include "jsobj.h"
namespace mongo {
LastError LastError::noError;
LastErrorHolder lastError;
2010-06-11 08:48:18 -04:00
bool isShell = false;
void raiseError(int code , const char *msg) {
LastError *le = lastError.get();
if ( le == 0 ) {
2011-01-04 00:40:41 -05:00
/* might be intentional (non-user thread) */
2010-08-17 16:41:17 -04:00
DEV {
static unsigned n;
2010-08-30 12:36:39 -04:00
if( ++n < 4 && !isShell ) log() << "dev: lastError==0 won't report:" << msg << endl;
2010-08-17 16:41:17 -04:00
}
2011-01-04 00:40:41 -05:00
}
else if ( le->disabled ) {
log() << "lastError disabled, can't report: " << code << ":" << msg << endl;
2011-01-04 00:40:41 -05:00
}
else {
2010-06-11 08:48:18 -04:00
le->raiseError(code, msg);
}
}
2010-08-10 14:56:33 -07:00
bool LastError::appendSelf( BSONObjBuilder &b , bool blankErr ) {
if ( !valid ) {
if ( blankErr )
b.appendNull( "err" );
b.append( "n", 0 );
return false;
}
if ( msg.empty() ) {
if ( blankErr ) {
2011-01-04 00:40:41 -05:00
b.appendNull( "err" );
}
}
else {
b.append( "err", msg );
}
2009-12-28 17:12:49 -05:00
if ( code )
b.append( "code" , code );
if ( updatedExisting != NotUpdate )
b.appendBool( "updatedExisting", updatedExisting == True );
if ( upsertedId.isSet() )
b.append( "upserted" , upsertedId );
if ( writebackId.isSet() ) {
2010-07-22 13:52:42 -04:00
b.append( "writeback" , writebackId );
b.append( "instanceIdent" , prettyHostName() ); // this can be any unique string
}
2010-04-27 15:58:45 -04:00
b.appendNumber( "n", nObjects );
2011-01-04 00:40:41 -05:00
return ! msg.empty();
}
2011-01-04 00:40:41 -05:00
LastErrorHolder::~LastErrorHolder() {
2010-08-10 14:56:33 -07:00
}
LastError * LastErrorHolder::disableForCommand() {
LastError *le = _get();
uassert(13649, "no operation yet", le);
le->disabled = true;
le->nPrev--; // caller is a command that shouldn't count as an operation
return le;
}
LastError * LastErrorHolder::get( bool create ) {
LastError *ret = _get( create );
if ( ret && !ret->disabled )
return ret;
return 0;
}
2011-01-04 00:40:41 -05:00
LastError * LastErrorHolder::_get( bool create ) {
LastError * le = _tl.get();
if ( ! le && create ) {
le = new LastError();
_tl.reset( le );
}
return le;
}
2011-01-04 00:40:41 -05:00
void LastErrorHolder::release() {
_tl.release();
}
2010-05-18 14:47:11 -04:00
/** ok to call more than once. */
2011-01-04 00:40:41 -05:00
void LastErrorHolder::initThread() {
if( ! _tl.get() )
_tl.reset( new LastError() );
2010-05-18 14:47:11 -04:00
}
2011-01-04 00:40:41 -05:00
void LastErrorHolder::reset( LastError * le ) {
_tl.reset( le );
}
2011-01-04 00:40:41 -05:00
void prepareErrForNewRequest( Message &m, LastError * err ) {
// a killCursors message shouldn't affect last error
assert( err );
2010-05-12 15:26:00 -07:00
if ( m.operation() == dbKillCursors ) {
err->disabled = true;
2011-01-04 00:40:41 -05:00
}
else {
err->disabled = false;
err->nPrev++;
2011-01-04 00:40:41 -05:00
}
}
2011-01-04 00:40:41 -05:00
LastError * LastErrorHolder::startRequest( Message& m , LastError * le ) {
assert( le );
prepareErrForNewRequest( m, le );
return le;
}
} // namespace mongo