24 #include "mongo/base/status.h"
25 #include "mongo/bson/inline_decls.h"
26 #include "mongo/platform/compiler.h"
30 enum CommonErrorCodes {
32 DatabaseDifferCaseCode = 13297 ,
33 SendStaleConfigCode = 13388 ,
34 RecvStaleConfigCode = 9996,
35 PrepareConfigsFailedCode = 13104
42 void condrollover(
int newValue );
58 : msg( m ) , code( c ) {
61 : msg( m ) , code( c ) {
63 void append(
BSONObjBuilder& b ,
const char * m =
"$err" ,
const char * c =
"code" )
const ;
64 std::string toString()
const;
65 bool empty()
const {
return msg.empty(); }
66 void reset(){ msg =
""; code=-1; }
80 ErrorMsg(
const char *msg,
unsigned val);
81 operator std::string()
const {
return buf; }
88 std::string causedBy(
const std::string& e );
95 DBException(
const char * msg ,
int code ) : _ei(msg,code) { traceIfNeeded(*
this); }
96 DBException(
const std::string& msg ,
int code ) : _ei(msg,code) { traceIfNeeded(*
this); }
99 virtual const char* what()
const throw() {
return _ei.msg.c_str(); }
100 virtual int getCode()
const {
return _ei.code; }
101 virtual void appendPrefix( std::stringstream& ss )
const { }
102 virtual void addContext(
const std::string& str ) {
103 _ei.msg = str + causedBy( _ei.msg );
107 static ErrorCodes::Error convertExceptionCode(
int exCode);
109 Status toStatus(
const std::string& context)
const {
110 return Status(convertExceptionCode(getCode()), context + causedBy(*
this));
112 Status toStatus()
const {
113 return Status(convertExceptionCode(getCode()), this->toString());
119 virtual std::string toString()
const;
125 static bool traceExceptions;
140 virtual bool severe() {
return true; }
141 virtual bool isUserAssertion() {
return false; }
145 return _ei.code == 11600 || _ei.code == 11601;
153 virtual bool severe() {
return false; }
154 virtual bool isUserAssertion() {
return true; }
155 virtual void appendPrefix( std::stringstream& ss )
const;
162 virtual bool severe() {
return false; }
163 virtual void appendPrefix( std::stringstream& ss )
const;
166 MONGO_COMPILER_NORETURN
void verifyFailed(
const char *msg,
const char *file,
unsigned line);
167 void wasserted(
const char *msg,
const char *file,
unsigned line);
168 MONGO_COMPILER_NORETURN
void fassertFailed(
int msgid );
169 MONGO_COMPILER_NORETURN
void fassertFailedNoTrace(
int msgid );
174 MONGO_COMPILER_NORETURN
void uasserted(
int msgid,
const char *msg);
175 MONGO_COMPILER_NORETURN
void uasserted(
int msgid ,
const std::string &msg);
181 MONGO_COMPILER_NORETURN
inline void msgassertedNoTrace(
int msgid,
const std::string& msg) {
184 MONGO_COMPILER_NORETURN
void msgasserted(
int msgid,
const char *msg);
185 MONGO_COMPILER_NORETURN
void msgasserted(
int msgid,
const std::string &msg);
188 inline std::string causedBy(
const char* e ){
return (std::string)
" :: caused by :: " + e; }
189 inline std::string causedBy(
const DBException& e ){
return causedBy( e.toString().c_str() ); }
190 inline std::string causedBy(
const std::exception& e ){
return causedBy( e.what() ); }
191 inline std::string causedBy(
const std::string& e ){
return causedBy( e.c_str() ); }
192 inline std::string causedBy(
const std::string* e ){
193 return (e && *e !=
"") ? causedBy(*e) :
"";
195 inline std::string causedBy(
const Status& e ){
return causedBy( e.reason() ); }
198 inline void fassert(
int msgid,
bool testOK) {
if (MONGO_unlikely(!testOK)) fassertFailed(msgid);}
202 #define MONGO_uassert(msgid, msg, expr) (void)( MONGO_likely(!!(expr)) || (mongo::uasserted(msgid, msg), 0) )
204 #define MONGO_uassertStatusOK(expr) do { \
205 Status status = (expr); \
206 if (!status.isOK()) \
207 uasserted(status.location() != 0 ? status.location() : status.code(), \
212 #define MONGO_wassert(_Expression) (void)( MONGO_likely(!!(_Expression)) || (mongo::wasserted(#_Expression, __FILE__, __LINE__), 0) )
219 #define MONGO_massert(msgid, msg, expr) (void)( MONGO_likely(!!(expr)) || (mongo::msgasserted(msgid, msg), 0) )
221 #define MONGO_verify(_Expression) (void)( MONGO_likely(!!(_Expression)) || (mongo::verifyFailed(#_Expression, __FILE__, __LINE__), 0) )
227 # define MONGO_dassert(x) fassert(16199, (x))
229 # define MONGO_dassert(x)
233 inline bool debugCompare(
bool inDebug,
bool condition) {
return inDebug && condition; }
236 # define MONGO_debug_and(x) debugCompare(true, (x))
238 # define MONGO_debug_and(x) debugCompare(false, (x))
241 #ifdef MONGO_EXPOSE_MACROS
242 # define dcompare MONGO_debug_and
243 # define dassert MONGO_dassert
244 # define verify MONGO_verify
245 # define uassert MONGO_uassert
246 # define uassertStatusOK MONGO_uassertStatusOK
247 # define wassert MONGO_wassert
248 # define massert MONGO_massert
256 enum { ASSERT_ID_DUPKEY = 11000 };
259 MONGO_COMPILER_NORETURN
void streamNotGood(
int code,
const std::string& msg, std::ios& myios );
261 inline void assertStreamGood(
unsigned msgid,
const std::string& msg, std::ios& myios) {
262 if( !myios.good() ) streamNotGood(msgid, msg, myios);
265 std::string demangleName(
const std::type_info& typeinfo );
269 #define MONGO_ASSERT_ON_EXCEPTION( expression ) \
272 } catch ( const std::exception &e ) { \
274 ss << "caught exception: " << e.what() << ' ' << __FILE__ << ' ' << __LINE__; \
275 msgasserted( 13294 , ss.str() ); \
277 massert( 10437 , "unknown exception" , false ); \
280 #define MONGO_ASSERT_ON_EXCEPTION_WITH_MSG( expression, msg ) \
283 } catch ( const std::exception &e ) { \
285 ss << msg << " caught exception exception: " << e.what(); \
286 msgasserted( 14043 , ss.str() ); \
288 msgasserted( 14044 , std::string("unknown exception") + msg ); \
291 #define DESTRUCTOR_GUARD MONGO_DESTRUCTOR_GUARD
292 #define MONGO_DESTRUCTOR_GUARD( expression ) \
295 } catch ( const std::exception &e ) { \
296 problem() << "caught exception (" << e.what() << ") in destructor (" << __FUNCTION__ << ")" << endl; \
298 problem() << "caught unknown exception in destructor (" << __FUNCTION__ << ")" << endl; \