error_macros.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*************************************************************************/
  2. /* error_macros.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef ERROR_MACROS_H
  31. #define ERROR_MACROS_H
  32. #include "core/typedefs.h"
  33. #include "core/templates/safe_refcount.h"
  34. class String;
  35. enum ErrorHandlerType {
  36. ERR_HANDLER_ERROR,
  37. ERR_HANDLER_WARNING,
  38. ERR_HANDLER_SCRIPT,
  39. ERR_HANDLER_SHADER,
  40. };
  41. // Pointer to the error handler printing function. Reassign to any function to have errors printed.
  42. // Parameters: userdata, function, file, line, error, explanation, type.
  43. typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
  44. struct ErrorHandlerList {
  45. ErrorHandlerFunc errfunc = nullptr;
  46. void *userdata = nullptr;
  47. ErrorHandlerList *next = nullptr;
  48. ErrorHandlerList() {}
  49. };
  50. void add_error_handler(ErrorHandlerList *p_handler);
  51. void remove_error_handler(ErrorHandlerList *p_handler);
  52. // Functions used by the error macros.
  53. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  54. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  55. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const char *p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  56. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const char *p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  57. void _err_print_error(const char *p_function, const char *p_file, int p_line, const char *p_error, const String &p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  58. void _err_print_error(const char *p_function, const char *p_file, int p_line, const String &p_error, const String &p_message, ErrorHandlerType p_type = ERR_HANDLER_ERROR);
  59. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const char *p_message = "", bool fatal = false);
  60. void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, const String &p_message, bool fatal = false);
  61. #ifdef __GNUC__
  62. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  63. #define FUNCTION_STR __FUNCTION__
  64. #else
  65. #define FUNCTION_STR __FUNCTION__
  66. #endif
  67. #ifdef _MSC_VER
  68. /**
  69. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  70. */
  71. #define GENERATE_TRAP() __debugbreak()
  72. #else
  73. /**
  74. * Don't use GENERATE_TRAP() directly, should only be used be the macros below.
  75. */
  76. #define GENERATE_TRAP() __builtin_trap()
  77. #endif
  78. // Used to strip debug messages in release mode
  79. #ifdef DEBUG_ENABLED
  80. #define DEBUG_STR(m_msg) m_msg
  81. #else
  82. #define DEBUG_STR(m_msg) ""
  83. #endif
  84. /**
  85. * Error macros.
  86. * WARNING: These macros work in the opposite way to assert().
  87. *
  88. * Unlike exceptions and asserts, these macros try to maintain consistency and stability.
  89. * In most cases, bugs and/or invalid data are not fatal. They should never allow a perfectly
  90. * running application to fail or crash.
  91. * Always try to return processable data, so the engine can keep running well.
  92. * Use the _MSG versions to print a meaningful message to help with debugging.
  93. *
  94. * The `((void)0)` no-op statement is used as a trick to force us to put a semicolon after
  95. * those macros, making them look like proper statements.
  96. * The if wrappers are used to ensure that the macro replacement does not trigger unexpected
  97. * issues when expanded e.g. after an `if (cond) ERR_FAIL();` without braces.
  98. */
  99. // Index out of bounds error macros.
  100. // These macros should be used instead of `ERR_FAIL_COND` for bounds checking.
  101. // Integer index out of bounds error macros.
  102. /**
  103. * Try using `ERR_FAIL_INDEX_MSG`.
  104. * Only use this macro if there is no sensible error message.
  105. *
  106. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  107. * If not, the current function returns.
  108. */
  109. #define ERR_FAIL_INDEX(m_index, m_size) \
  110. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  111. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  112. return; \
  113. } else \
  114. ((void)0)
  115. /**
  116. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  117. * If not, prints `m_msg` and the current function returns.
  118. */
  119. #define ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
  120. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  121. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  122. return; \
  123. } else \
  124. ((void)0)
  125. /**
  126. * Try using `ERR_FAIL_INDEX_V_MSG`.
  127. * Only use this macro if there is no sensible error message.
  128. *
  129. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  130. * If not, the current function returns `m_retval`.
  131. */
  132. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  133. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  134. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  135. return m_retval; \
  136. } else \
  137. ((void)0)
  138. /**
  139. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  140. * If not, prints `m_msg` and the current function returns `m_retval`.
  141. */
  142. #define ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  143. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  144. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  145. return m_retval; \
  146. } else \
  147. ((void)0)
  148. /**
  149. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  150. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  151. * there is no sensible error message.
  152. *
  153. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  154. * If not, the application crashes.
  155. */
  156. #define CRASH_BAD_INDEX(m_index, m_size) \
  157. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  158. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  159. GENERATE_TRAP(); \
  160. } else \
  161. ((void)0)
  162. /**
  163. * Try using `ERR_FAIL_INDEX_MSG` or `ERR_FAIL_INDEX_V_MSG`.
  164. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  165. *
  166. * Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
  167. * If not, prints `m_msg` and the application crashes.
  168. */
  169. #define CRASH_BAD_INDEX_MSG(m_index, m_size, m_msg) \
  170. if (unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
  171. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg), true); \
  172. GENERATE_TRAP(); \
  173. } else \
  174. ((void)0)
  175. // Unsigned integer index out of bounds error macros.
  176. /**
  177. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG`.
  178. * Only use this macro if there is no sensible error message.
  179. *
  180. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  181. * If not, the current function returns.
  182. */
  183. #define ERR_FAIL_UNSIGNED_INDEX(m_index, m_size) \
  184. if (unlikely((m_index) >= (m_size))) { \
  185. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  186. return; \
  187. } else \
  188. ((void)0)
  189. /**
  190. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  191. * If not, prints `m_msg` and the current function returns.
  192. */
  193. #define ERR_FAIL_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  194. if (unlikely((m_index) >= (m_size))) { \
  195. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  196. return; \
  197. } else \
  198. ((void)0)
  199. /**
  200. * Try using `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  201. * Only use this macro if there is no sensible error message.
  202. *
  203. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  204. * If not, the current function returns `m_retval`.
  205. */
  206. #define ERR_FAIL_UNSIGNED_INDEX_V(m_index, m_size, m_retval) \
  207. if (unlikely((m_index) >= (m_size))) { \
  208. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size)); \
  209. return m_retval; \
  210. } else \
  211. ((void)0)
  212. /**
  213. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  214. * If not, prints `m_msg` and the current function returns `m_retval`.
  215. */
  216. #define ERR_FAIL_UNSIGNED_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
  217. if (unlikely((m_index) >= (m_size))) { \
  218. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg)); \
  219. return m_retval; \
  220. } else \
  221. ((void)0)
  222. /**
  223. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  224. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  225. * there is no sensible error message.
  226. *
  227. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  228. * If not, the application crashes.
  229. */
  230. #define CRASH_BAD_UNSIGNED_INDEX(m_index, m_size) \
  231. if (unlikely((m_index) >= (m_size))) { \
  232. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), "", true); \
  233. GENERATE_TRAP(); \
  234. } else \
  235. ((void)0)
  236. /**
  237. * Try using `ERR_FAIL_UNSIGNED_INDEX_MSG` or `ERR_FAIL_UNSIGNED_INDEX_V_MSG`.
  238. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  239. *
  240. * Ensures an unsigned integer index `m_index` is less than `m_size`.
  241. * If not, prints `m_msg` and the application crashes.
  242. */
  243. #define CRASH_BAD_UNSIGNED_INDEX_MSG(m_index, m_size, m_msg) \
  244. if (unlikely((m_index) >= (m_size))) { \
  245. _err_print_index_error(FUNCTION_STR, __FILE__, __LINE__, m_index, m_size, _STR(m_index), _STR(m_size), DEBUG_STR(m_msg), true); \
  246. GENERATE_TRAP(); \
  247. } else \
  248. ((void)0)
  249. // Null reference error macros.
  250. /**
  251. * Try using `ERR_FAIL_NULL_MSG`.
  252. * Only use this macro if there is no sensible error message.
  253. *
  254. * Ensures a pointer `m_param` is not null.
  255. * If it is null, the current function returns.
  256. */
  257. #define ERR_FAIL_NULL(m_param) \
  258. if (unlikely(m_param == nullptr)) { \
  259. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  260. return; \
  261. } else \
  262. ((void)0)
  263. /**
  264. * Ensures a pointer `m_param` is not null.
  265. * If it is null, prints `m_msg` and the current function returns.
  266. */
  267. #define ERR_FAIL_NULL_MSG(m_param, m_msg) \
  268. if (unlikely(m_param == nullptr)) { \
  269. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", DEBUG_STR(m_msg)); \
  270. return; \
  271. } else \
  272. ((void)0)
  273. /**
  274. * Try using `ERR_FAIL_NULL_V_MSG`.
  275. * Only use this macro if there is no sensible error message.
  276. *
  277. * Ensures a pointer `m_param` is not null.
  278. * If it is null, the current function returns `m_retval`.
  279. */
  280. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  281. if (unlikely(m_param == nullptr)) { \
  282. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null."); \
  283. return m_retval; \
  284. } else \
  285. ((void)0)
  286. /**
  287. * Ensures a pointer `m_param` is not null.
  288. * If it is null, prints `m_msg` and the current function returns `m_retval`.
  289. */
  290. #define ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
  291. if (unlikely(m_param == nullptr)) { \
  292. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter \"" _STR(m_param) "\" is null.", DEBUG_STR(m_msg)); \
  293. return m_retval; \
  294. } else \
  295. ((void)0)
  296. /**
  297. * Try using `ERR_FAIL_COND_MSG`.
  298. * Only use this macro if there is no sensible error message.
  299. * If checking for null use ERR_FAIL_NULL_MSG instead.
  300. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  301. *
  302. * Ensures `m_cond` is false.
  303. * If `m_cond` is true, the current function returns.
  304. */
  305. #define ERR_FAIL_COND(m_cond) \
  306. if (unlikely(m_cond)) { \
  307. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true."); \
  308. return; \
  309. } else \
  310. ((void)0)
  311. /**
  312. * Ensures `m_cond` is false.
  313. * If `m_cond` is true, prints `m_msg` and the current function returns.
  314. *
  315. * If checking for null use ERR_FAIL_NULL_MSG instead.
  316. * If checking index bounds use ERR_FAIL_INDEX_MSG instead.
  317. */
  318. #define ERR_FAIL_COND_MSG(m_cond, m_msg) \
  319. if (unlikely(m_cond)) { \
  320. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true.", DEBUG_STR(m_msg)); \
  321. return; \
  322. } else \
  323. ((void)0)
  324. /**
  325. * Try using `ERR_FAIL_COND_V_MSG`.
  326. * Only use this macro if there is no sensible error message.
  327. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  328. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  329. *
  330. * Ensures `m_cond` is false.
  331. * If `m_cond` is true, the current function returns `m_retval`.
  332. */
  333. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  334. if (unlikely(m_cond)) { \
  335. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval)); \
  336. return m_retval; \
  337. } else \
  338. ((void)0)
  339. /**
  340. * Ensures `m_cond` is false.
  341. * If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
  342. *
  343. * If checking for null use ERR_FAIL_NULL_V_MSG instead.
  344. * If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
  345. */
  346. #define ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
  347. if (unlikely(m_cond)) { \
  348. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Returning: " _STR(m_retval), DEBUG_STR(m_msg)); \
  349. return m_retval; \
  350. } else \
  351. ((void)0)
  352. /**
  353. * Try using `ERR_CONTINUE_MSG`.
  354. * Only use this macro if there is no sensible error message.
  355. *
  356. * Ensures `m_cond` is false.
  357. * If `m_cond` is true, the current loop continues.
  358. */
  359. #define ERR_CONTINUE(m_cond) \
  360. if (unlikely(m_cond)) { \
  361. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing."); \
  362. continue; \
  363. } else \
  364. ((void)0)
  365. /**
  366. * Ensures `m_cond` is false.
  367. * If `m_cond` is true, prints `m_msg` and the current loop continues.
  368. */
  369. #define ERR_CONTINUE_MSG(m_cond, m_msg) \
  370. if (unlikely(m_cond)) { \
  371. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Continuing.", DEBUG_STR(m_msg)); \
  372. continue; \
  373. } else \
  374. ((void)0)
  375. /**
  376. * Try using `ERR_BREAK_MSG`.
  377. * Only use this macro if there is no sensible error message.
  378. *
  379. * Ensures `m_cond` is false.
  380. * If `m_cond` is true, the current loop breaks.
  381. */
  382. #define ERR_BREAK(m_cond) \
  383. if (unlikely(m_cond)) { \
  384. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking."); \
  385. break; \
  386. } else \
  387. ((void)0)
  388. /**
  389. * Ensures `m_cond` is false.
  390. * If `m_cond` is true, prints `m_msg` and the current loop breaks.
  391. */
  392. #define ERR_BREAK_MSG(m_cond, m_msg) \
  393. if (unlikely(m_cond)) { \
  394. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition \"" _STR(m_cond) "\" is true. Breaking.", DEBUG_STR(m_msg)); \
  395. break; \
  396. } else \
  397. ((void)0)
  398. /**
  399. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  400. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable, and
  401. * there is no sensible error message.
  402. *
  403. * Ensures `m_cond` is false.
  404. * If `m_cond` is true, the application crashes.
  405. */
  406. #define CRASH_COND(m_cond) \
  407. if (unlikely(m_cond)) { \
  408. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true."); \
  409. GENERATE_TRAP(); \
  410. } else \
  411. ((void)0)
  412. /**
  413. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_COND_V_MSG`.
  414. * Only use this macro if there is no sensible fallback i.e. the error is unrecoverable.
  415. *
  416. * Ensures `m_cond` is false.
  417. * If `m_cond` is true, prints `m_msg` and the application crashes.
  418. */
  419. #define CRASH_COND_MSG(m_cond, m_msg) \
  420. if (unlikely(m_cond)) { \
  421. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Condition \"" _STR(m_cond) "\" is true.", DEBUG_STR(m_msg)); \
  422. GENERATE_TRAP(); \
  423. } else \
  424. ((void)0)
  425. // Generic error macros.
  426. /**
  427. * Try using `ERR_FAIL_COND_MSG` or `ERR_FAIL_MSG`.
  428. * Only use this macro if more complex error detection or recovery is required, and
  429. * there is no sensible error message.
  430. *
  431. * The current function returns.
  432. */
  433. #define ERR_FAIL() \
  434. if (true) { \
  435. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed."); \
  436. return; \
  437. } else \
  438. ((void)0)
  439. /**
  440. * Try using `ERR_FAIL_COND_MSG`.
  441. * Only use this macro if more complex error detection or recovery is required.
  442. *
  443. * Prints `m_msg`, and the current function returns.
  444. */
  445. #define ERR_FAIL_MSG(m_msg) \
  446. if (true) { \
  447. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed.", DEBUG_STR(m_msg)); \
  448. return; \
  449. } else \
  450. ((void)0)
  451. /**
  452. * Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
  453. * Only use this macro if more complex error detection or recovery is required, and
  454. * there is no sensible error message.
  455. *
  456. * The current function returns `m_retval`.
  457. */
  458. #define ERR_FAIL_V(m_retval) \
  459. if (true) { \
  460. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval)); \
  461. return m_retval; \
  462. } else \
  463. ((void)0)
  464. /**
  465. * Try using `ERR_FAIL_COND_V_MSG`.
  466. * Only use this macro if more complex error detection or recovery is required.
  467. *
  468. * Prints `m_msg`, and the current function returns `m_retval`.
  469. */
  470. #define ERR_FAIL_V_MSG(m_retval, m_msg) \
  471. if (true) { \
  472. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/function failed. Returning: " _STR(m_retval), DEBUG_STR(m_msg)); \
  473. return m_retval; \
  474. } else \
  475. ((void)0)
  476. /**
  477. * Try using `ERR_FAIL_COND_MSG`, `ERR_FAIL_COND_V_MSG`, `ERR_CONTINUE_MSG` or ERR_BREAK_MSG.
  478. * Only use this macro at the start of a function that has not been implemented yet, or
  479. * if more complex error detection or recovery is required.
  480. *
  481. * Prints `m_msg`.
  482. */
  483. #define ERR_PRINT(m_msg) \
  484. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg)
  485. /**
  486. * Prints `m_msg` once during the application lifetime.
  487. */
  488. #define ERR_PRINT_ONCE(m_msg) \
  489. if (true) { \
  490. static bool first_print = true; \
  491. if (first_print) { \
  492. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg); \
  493. first_print = false; \
  494. } \
  495. } else \
  496. ((void)0)
  497. // Print warning message macros.
  498. /**
  499. * Prints `m_msg`.
  500. *
  501. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  502. */
  503. #define WARN_PRINT(m_msg) \
  504. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, ERR_HANDLER_WARNING)
  505. /**
  506. * Prints `m_msg` once during the application lifetime.
  507. *
  508. * If warning about deprecated usage, use `WARN_DEPRECATED` or `WARN_DEPRECATED_MSG` instead.
  509. */
  510. #define WARN_PRINT_ONCE(m_msg) \
  511. if (true) { \
  512. static bool first_print = true; \
  513. if (first_print) { \
  514. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_msg, ERR_HANDLER_WARNING); \
  515. first_print = false; \
  516. } \
  517. } else \
  518. ((void)0)
  519. // Print deprecated warning message macros.
  520. /**
  521. * Warns that the current function is deprecated.
  522. */
  523. #define WARN_DEPRECATED \
  524. if (true) { \
  525. static SafeFlag warning_shown; \
  526. if (!warning_shown.is_set()) { \
  527. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", ERR_HANDLER_WARNING); \
  528. warning_shown.set(); \
  529. } \
  530. } else \
  531. ((void)0)
  532. /**
  533. * Warns that the current function is deprecated and prints `m_msg`.
  534. */
  535. #define WARN_DEPRECATED_MSG(m_msg) \
  536. if (true) { \
  537. static SafeFlag warning_shown; \
  538. if (!warning_shown.is_set()) { \
  539. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "This method has been deprecated and will be removed in the future.", DEBUG_STR(m_msg), ERR_HANDLER_WARNING); \
  540. warning_shown.set(); \
  541. } \
  542. } else \
  543. ((void)0)
  544. /**
  545. * Do not use.
  546. * If the application should never reach this point use CRASH_NOW_MSG(m_msg) to explain why.
  547. *
  548. * The application crashes.
  549. */
  550. #define CRASH_NOW() \
  551. if (true) { \
  552. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed."); \
  553. GENERATE_TRAP(); \
  554. } else \
  555. ((void)0)
  556. /**
  557. * Only use if the application should never reach this point.
  558. *
  559. * Prints `m_msg`, and then the application crashes.
  560. */
  561. #define CRASH_NOW_MSG(m_msg) \
  562. if (true) { \
  563. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "FATAL: Method/function failed.", DEBUG_STR(m_msg)); \
  564. GENERATE_TRAP(); \
  565. } else \
  566. ((void)0)
  567. #endif // ERROR_MACROS_H