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,
47 ExceptionInfo(
const char* m,
int c) : msg(m), code(c) {}
48 ExceptionInfo(
const std::string& m,
int c) : msg(m), code(c) {}
49 void append(
BSONObjBuilder& b,
const char* m =
"$err",
const char* c =
"code")
const;
50 std::string toString()
const;
73 ErrorMsg(
const char* msg,
unsigned val);
74 operator std::string()
const {
83 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const DBException& e);
84 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::string& e);
90 DBException(
const char* msg,
int code) : _ei(msg, code) {}
91 DBException(
const std::string& msg,
int code) : _ei(msg, code) {}
94 virtual const char* what()
const throw() {
95 return _ei.msg.c_str();
97 virtual int getCode()
const {
100 virtual void appendPrefix(std::stringstream& ss)
const {}
101 virtual void addContext(
const std::string& str) {
102 _ei.msg = str + causedBy(_ei.msg);
106 static ErrorCodes::Error MONGO_CLIENT_FUNC convertExceptionCode(
int exCode);
108 Status toStatus(
const std::string& context)
const {
109 return Status(convertExceptionCode(getCode()), context + causedBy(*
this));
112 return Status(convertExceptionCode(getCode()), this->what());
118 virtual std::string toString()
const;
136 virtual bool severe()
const {
139 virtual bool isUserAssertion()
const {
148 virtual bool severe()
const {
151 virtual bool isUserAssertion()
const {
154 virtual void appendPrefix(std::stringstream& ss)
const;
161 virtual bool severe()
const {
164 virtual void appendPrefix(std::stringstream& ss)
const;
167 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
168 verifyFailed(
const char* expr,
const char* file,
unsigned line);
169 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
170 invariantFailed(
const char* expr,
const char* file,
unsigned line);
171 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
172 invariantOKFailed(
const char* expr,
const Status& status,
const char* file,
unsigned line);
173 MONGO_CLIENT_API
void MONGO_CLIENT_FUNC
174 wasserted(
const char* expr,
const char* file,
unsigned line);
175 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC fassertFailed(
int msgid);
176 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
177 fassertFailedWithStatus(
int msgid,
const Status& status);
178 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
179 fassertFailedWithStatusNoTrace(
int msgid,
const Status& status);
184 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
186 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
187 uasserted(
int msgid,
const std::string& msg);
192 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
194 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
196 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
197 msgasserted(
int msgid,
const char* msg);
198 MONGO_CLIENT_API MONGO_COMPILER_NORETURN
void MONGO_CLIENT_FUNC
199 msgasserted(
int msgid,
const std::string& msg);
202 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const char* e);
203 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const DBException& e);
204 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::exception& e);
205 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::string& e);
206 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const std::string* e);
207 MONGO_CLIENT_API std::string MONGO_CLIENT_FUNC causedBy(
const Status& e);
210 MONGO_CLIENT_API
inline void MONGO_CLIENT_FUNC
fassert(
int msgid,
bool testOK) {
211 if (MONGO_unlikely(!testOK))
212 fassertFailed(msgid);
215 MONGO_CLIENT_API
inline void MONGO_CLIENT_FUNC
fassert(
int msgid,
const Status& status) {
216 if (MONGO_unlikely(!status.isOK())) {
217 fassertFailedWithStatus(msgid, status);
221 MONGO_CLIENT_API
inline void fassertNoTrace(
int msgid,
const Status& status) {
222 if (MONGO_unlikely(!status.isOK())) {
223 fassertFailedWithStatusNoTrace(msgid, status);
229 #define MONGO_uassert(msgid, msg, expr) \
231 if (MONGO_unlikely(!(expr))) { \
232 ::mongo::uasserted(msgid, msg); \
236 MONGO_CLIENT_API
inline void MONGO_CLIENT_FUNC uassertStatusOK(
const Status& status) {
237 if (MONGO_unlikely(!status.isOK())) {
238 uasserted((status.location() != 0 ? status.location() : status.code()), status.reason());
243 #define MONGO_wassert(_Expression) \
245 if (MONGO_unlikely(!(_Expression))) { \
246 ::mongo::wasserted(#_Expression, __FILE__, __LINE__); \
255 #define MONGO_massert(msgid, msg, expr) \
257 if (MONGO_unlikely(!(expr))) { \
258 ::mongo::msgasserted(msgid, msg); \
262 MONGO_CLIENT_API
inline void massertStatusOK(
const Status& status) {
263 if (MONGO_unlikely(!status.isOK())) {
264 msgasserted((status.location() != 0 ? status.location() : status.code()), status.reason());
268 MONGO_CLIENT_API
inline void massertNoTraceStatusOK(
const Status& status) {
269 if (MONGO_unlikely(!status.isOK())) {
276 #define MONGO_verify(_Expression) \
278 if (MONGO_unlikely(!(_Expression))) { \
279 ::mongo::verifyFailed(#_Expression, __FILE__, __LINE__); \
283 #define MONGO_invariant(_Expression) \
285 if (MONGO_unlikely(!(_Expression))) { \
286 ::mongo::invariantFailed(#_Expression, __FILE__, __LINE__); \
290 #define MONGO_invariantOK(expression) \
292 const ::mongo::Status _invariantOK_status = expression; \
293 if (MONGO_unlikely(!_invariantOK_status.isOK())) { \
294 ::mongo::invariantOKFailed(#expression, _invariantOK_status, __FILE__, __LINE__); \
298 #ifdef MONGO_EXPOSE_MACROS
299 #define verify(expression) MONGO_verify(expression)
300 #define invariant MONGO_invariant
301 #define invariantOK MONGO_invariantOK
302 #define uassert MONGO_uassert
303 #define wassert MONGO_wassert
304 #define massert MONGO_massert
312 enum { ASSERT_ID_DUPKEY = 11000 };
314 std::string demangleName(
const std::type_info& typeinfo);
318 #define MONGO_ASSERT_ON_EXCEPTION(expression) \
321 } catch (const std::exception& e) { \
322 std::stringstream ss; \
323 ss << "caught exception: " << e.what() << ' ' << __FILE__ << ' ' << __LINE__; \
324 msgasserted(13294, ss.str()); \
326 massert(10437, "unknown exception", false); \
329 #define MONGO_ASSERT_ON_EXCEPTION_WITH_MSG(expression, msg) \
332 } catch (const std::exception& e) { \
333 std::stringstream ss; \
334 ss << msg << " caught exception exception: " << e.what(); \
335 msgasserted(14043, ss.str()); \
337 msgasserted(14044, std::string("unknown exception") + msg); \
340 #define DESTRUCTOR_GUARD MONGO_DESTRUCTOR_GUARD
341 #define MONGO_DESTRUCTOR_GUARD(expression) \
344 } catch (const std::exception& e) { \
345 ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
347 ::mongo::logger::LogSeverity::Log()) \
348 << "caught exception (" << e.what() << ") in destructor (" << __FUNCTION__ << ")" \
351 ::mongo::logger::LogstreamBuilder(::mongo::logger::globalLogDomain(), \
353 ::mongo::logger::LogSeverity::Log()) \
354 << "caught unknown exception in destructor (" << __FUNCTION__ << ")" << std::endl; \
369 #define MONGO_UNREACHABLE ::mongo::invariantFailed("Hit a MONGO_UNREACHABLE!", __FILE__, __LINE__);
Status represents an error state or the absence thereof.
Definition: status.h:50
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Definition: assert_util.h:45
helper class that builds error strings.
Definition: assert_util.h:70
MONGO_CLIENT_API MONGO_COMPILER_NORETURN void MONGO_CLIENT_FUNC uasserted(int msgid, const char *msg)
a "user assertion".
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
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:128
MONGO_CLIENT_API void MONGO_CLIENT_FUNC fassert(int msgid, bool testOK)
aborts on condition failure
Definition: assert_util.h:210
Definition: assert_util.h:145
Most mongo exceptions inherit from this; this is commonly caught in most threads. ...
Definition: assert_util.h:87
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.
Definition: assert_util.h:157