1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// 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.
*/
#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;
bool isShell = false;
void raiseError(int code , const char *msg) {
LastError *le = lastError.get();
if ( le == 0 ) {
/* might be intentional (non-user thread) */
DEV {
static unsigned n;
if( ++n < 4 && !isShell ) log() << "dev: lastError==0 won't report:" << msg << endl;
}
}
else if ( le->disabled ) {
log() << "lastError disabled, can't report: " << code << ":" << msg << endl;
}
else {
le->raiseError(code, msg);
}
}
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 ) {
b.appendNull( "err" );
}
}
else {
b.append( "err", msg );
}
if ( code )
b.append( "code" , code );
if ( updatedExisting != NotUpdate )
b.appendBool( "updatedExisting", updatedExisting == True );
if ( upsertedId.isSet() )
b.append( "upserted" , upsertedId );
if ( writebackId.isSet() ) {
b.append( "writeback" , writebackId );
b.append( "instanceIdent" , prettyHostName() ); // this can be any unique string
}
b.appendNumber( "n", nObjects );
return ! msg.empty();
}
LastErrorHolder::~LastErrorHolder() {
}
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;
}
LastError * LastErrorHolder::_get( bool create ) {
LastError * le = _tl.get();
if ( ! le && create ) {
le = new LastError();
_tl.reset( le );
}
return le;
}
void LastErrorHolder::release() {
_tl.release();
}
/** ok to call more than once. */
void LastErrorHolder::initThread() {
if( ! _tl.get() )
_tl.reset( new LastError() );
}
void LastErrorHolder::reset( LastError * le ) {
_tl.reset( le );
}
void prepareErrForNewRequest( Message &m, LastError * err ) {
// a killCursors message shouldn't affect last error
assert( err );
if ( m.operation() == dbKillCursors ) {
err->disabled = true;
}
else {
err->disabled = false;
err->nPrev++;
}
}
LastError * LastErrorHolder::startRequest( Message& m , LastError * le ) {
assert( le );
prepareErrForNewRequest( m, le );
return le;
}
} // namespace mongo
|