23#include "mongo/base/status.h"
24#include "mongo/client/export_macros.h"
25#include "mongo/platform/compiler.h"
26#include "mongo/logger/log_severity.h"
27#include "mongo/logger/logger.h"
28#include "mongo/logger/logstream_builder.h"
32 enum CommonErrorCodes {
34 DatabaseDifferCaseCode = 13297 ,
35 SendStaleConfigCode = 13388 ,
36 RecvStaleConfigCode = 9996,
37 PrepareConfigsFailedCode = 13104,
38 NotMasterOrSecondaryCode = 13436,
39 NotMasterNoSlaveOkCode = 13435,
48 : msg( m ) , code( c ) {
51 : msg( m ) , code( c ) {
53 void append(
BSONObjBuilder& b ,
const char * m =
"$err" ,
const char * c =
"code" )
const ;
54 std::string toString()
const;
55 bool empty()
const {
return msg.empty(); }
56 void reset(){ msg =
""; code=-1; }
70 ErrorMsg(
const char *msg,
unsigned val);
71 operator std::string()
const {
return buf; }
77 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const DBException& e );
78 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::string& e );
84 DBException(
const char * msg ,
int code ) : _ei(msg,code) {}
85 DBException(
const std::string& msg ,
int code ) : _ei(msg,code) {}
88 virtual const char* what()
const throw() {
return _ei.msg.c_str(); }
89 virtual int getCode()
const {
return _ei.code; }
90 virtual void appendPrefix( std::stringstream& ss )
const { }
91 virtual void addContext(
const std::string& str ) {
92 _ei.msg = str + causedBy( _ei.msg );
96 static ErrorCodes::Error MONGO_CLIENT_FUNC convertExceptionCode(
int exCode);
98 Status toStatus(
const std::string& context)
const {
99 return Status(convertExceptionCode(getCode()), context + causedBy(*
this));
102 return Status(convertExceptionCode(getCode()), this->what());
108 virtual std::string toString()
const;
125 virtual bool severe()
const {
return true; }
126 virtual bool isUserAssertion()
const {
return false; }
133 virtual bool severe()
const {
return false; }
134 virtual bool isUserAssertion()
const {
return true; }
135 virtual void appendPrefix( std::stringstream& ss )
const;
142 virtual bool severe()
const {
return false; }
143 virtual void appendPrefix( std::stringstream& ss )
const;
146 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC verifyFailed(
const char *expr,
const char *file,
unsigned line);
147 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC invariantFailed(
const char *expr,
const char *file,
unsigned line);
148 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC invariantOKFailed(
const char *expr,
const Status& status,
const char *file,
unsigned line);
149 MONGO_CLIENT_API
void MONGO_CLIENT_FUNC wasserted(
const char *expr,
const char *file,
unsigned line);
150 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC fassertFailed(
int msgid );
151 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC fassertFailedWithStatus(
152 int msgid,
const Status& status);
153 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC fassertFailedWithStatusNoTrace(
154 int msgid,
const Status& status);
159 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
uasserted(
int msgid,
const char *msg);
160 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
uasserted(
int msgid ,
const std::string &msg);
165 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
msgassertedNoTrace(
int msgid,
const char *msg);
166 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
msgassertedNoTrace(
int msgid,
const std::string &msg);
167 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC msgasserted(
int msgid,
const char *msg);
168 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC msgasserted(
int msgid,
const std::string &msg);
171 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const char* e );
172 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const DBException& e );
173 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::exception& e );
174 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::string& e );
175 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::string* e );
176 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const Status& e );
179 MONGO_CLIENT_API
inline void MONGO_CLIENT_FUNC
fassert(
int msgid,
bool testOK) {
180 if (MONGO_unlikely(!testOK)) fassertFailed(msgid);
183 MONGO_CLIENT_API
inline void MONGO_CLIENT_FUNC
fassert(
int msgid,
const Status& status) {
184 if (MONGO_unlikely(!status.isOK())) {
185 fassertFailedWithStatus(msgid, status);
189 MONGO_CLIENT_API
inline void fassertNoTrace(
int msgid,
const Status& status) {
190 if (MONGO_unlikely(!status.isOK())) {
191 fassertFailedWithStatusNoTrace(msgid, status);
197#define MONGO_uassert(msgid, msg, expr) do { \
198 if (MONGO_unlikely(!(expr))) { \
199 ::mongo::uasserted(msgid, msg); \
203 MONGO_CLIENT_API
inline void MONGO_CLIENT_FUNC uassertStatusOK(
const Status& status) {
204 if (MONGO_unlikely(!status.isOK())) {
205 uasserted((status.location() != 0 ? status.location() : status.code()),
211#define MONGO_wassert(_Expression) do { \
212 if (MONGO_unlikely(!(_Expression))) { \
213 ::mongo::wasserted(#_Expression, __FILE__, __LINE__); \
222#define MONGO_massert(msgid, msg, expr) do { \
223 if (MONGO_unlikely(!(expr))) { \
224 ::mongo::msgasserted(msgid, msg); \
228 MONGO_CLIENT_API
inline void massertStatusOK(
const Status& status) {
229 if (MONGO_unlikely(!status.isOK())) {
230 msgasserted((status.location() != 0 ? status.location() : status.code()),
235 MONGO_CLIENT_API
inline void massertNoTraceStatusOK(
const Status& status) {
236 if (MONGO_unlikely(!status.isOK())) {
243#define MONGO_verify(_Expression) do { \
244 if (MONGO_unlikely(!(_Expression))) { \
245 ::mongo::verifyFailed(#_Expression, __FILE__, __LINE__); \
249#define MONGO_invariant(_Expression) do { \
250 if (MONGO_unlikely(!(_Expression))) { \
251 ::mongo::invariantFailed(#_Expression, __FILE__, __LINE__); \
255#define MONGO_invariantOK(expression) do { \
256 const ::mongo::Status _invariantOK_status = expression; \
257 if (MONGO_unlikely(!_invariantOK_status.isOK())) { \
258 ::mongo::invariantOKFailed(#expression, _invariantOK_status, __FILE__, __LINE__); \
262#ifdef MONGO_EXPOSE_MACROS
263# define verify(expression) MONGO_verify(expression)
264# define invariant MONGO_invariant
265# define invariantOK MONGO_invariantOK
266# define uassert MONGO_uassert
267# define wassert MONGO_wassert
268# define massert MONGO_massert
276 enum { ASSERT_ID_DUPKEY = 11000 };
278 std::string demangleName(
const std::type_info& typeinfo );
282#define MONGO_ASSERT_ON_EXCEPTION( expression ) \
285 } catch ( const std::exception &e ) { \
286 std::stringstream ss; \
287 ss << "caught exception: " << e.what() << ' ' << __FILE__ << ' ' << __LINE__; \
288 msgasserted( 13294 , ss.str() ); \
290 massert( 10437 , "unknown exception" , false ); \
293#define MONGO_ASSERT_ON_EXCEPTION_WITH_MSG( expression, msg ) \
296 } catch ( const std::exception &e ) { \
297 std::stringstream ss; \
298 ss << msg << " caught exception exception: " << e.what(); \
299 msgasserted( 14043 , ss.str() ); \
301 msgasserted( 14044 , std::string("unknown exception") + msg ); \
304#define DESTRUCTOR_GUARD MONGO_DESTRUCTOR_GUARD
305#define MONGO_DESTRUCTOR_GUARD( expression ) \
308 } catch ( const std::exception &e ) { \
309 ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
311 ::mongo::logger::LogSeverity::Log()) \
312 << "caught exception (" << e.what() << ") in destructor (" << __FUNCTION__ \
313 << ")" << std::endl; \
315 ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
317 ::mongo::logger::LogSeverity::Log()) \
318 << "caught unknown exception in destructor (" << __FUNCTION__ << ")" \
Definition assert_util.h:116
Utility for creating a BSONObj.
Definition bsonobjbuilder.h:53
Most mongo exceptions inherit from this; this is commonly caught in most threads.
Definition assert_util.h:81
helper class that builds error strings.
Definition assert_util.h:67
Definition assert_util.h:138
Definition assert_util.h:130
the main MongoDB namespace
Definition bulk_operation_builder.h:24
MONGO_CLIENT_API MONGO_COMPILER_NORETURN void MONGO_CLIENT_FUNC uasserted(int msgid, const char *msg)
a "user assertion".
MONGO_CLIENT_API void MONGO_CLIENT_FUNC fassert(int msgid, bool testOK)
aborts on condition failure
Definition assert_util.h:179
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.
MONGO_CLIENT_API MONGO_COMPILER_NORETURN void MONGO_CLIENT_FUNC msgassertedNoTrace(int msgid, const char *msg)
msgassert and massert are for errors that are internal but have a well defined error text std::string...
Definition assert_util.h:45