error_macros.h 36 KB

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