error_macros.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*************************************************************************/
  2. /* error_macros.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. /**
  33. * Error macros. Unlike exceptions and asserts, these macros try to mantain consistency and stability
  34. * inside the code. It is recommended to always return processable data, so in case of an error, the
  35. * engine can stay working well.
  36. * In most cases, bugs and/or invalid data are not fatal and should never allow a perfectly running application
  37. * to fail or crash.
  38. */
  39. /**
  40. * Pointer to the error macro priting function. Reassign to any function to have errors printed
  41. */
  42. /** Function used by the error macros */
  43. // function, file, line, error, explanation
  44. enum ErrorHandlerType {
  45. ERR_HANDLER_ERROR,
  46. ERR_HANDLER_WARNING,
  47. ERR_HANDLER_SCRIPT
  48. };
  49. typedef void (*ErrorHandlerFunc)(void *, const char *, const char *, int p_line, const char *, const char *, ErrorHandlerType p_type);
  50. void _err_set_last_error(const char *p_err);
  51. void _err_clear_last_error();
  52. struct ErrorHandlerList {
  53. ErrorHandlerFunc errfunc;
  54. void *userdata;
  55. ErrorHandlerList *next;
  56. ErrorHandlerList() {
  57. errfunc = 0;
  58. next = 0;
  59. userdata = 0;
  60. }
  61. };
  62. void add_error_handler(ErrorHandlerList *p_handler);
  63. void remove_error_handler(ErrorHandlerList *p_handler);
  64. 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);
  65. #ifndef _STR
  66. #define _STR(m_x) #m_x
  67. #define _MKSTR(m_x) _STR(m_x)
  68. #endif
  69. #define _FNL __FILE__ ":"
  70. /** An index has failed if m_index<0 or m_index >=m_size, the function exists */
  71. extern bool _err_error_exists;
  72. #ifdef DEBUG_ENABLED
  73. /** Print a warning string.
  74. */
  75. #define ERR_EXPLAINC(m_reason) \
  76. { \
  77. _err_set_last_error(m_reason); \
  78. _err_error_exists = true; \
  79. }
  80. #define ERR_EXPLAIN(m_string) \
  81. { \
  82. _err_set_last_error(String(m_string).utf8().get_data()); \
  83. _err_error_exists = true; \
  84. }
  85. #else
  86. #define ERR_EXPLAIN(m_text)
  87. #define ERR_EXPLAINC(m_text)
  88. #endif
  89. #ifdef __GNUC__
  90. //#define FUNCTION_STR __PRETTY_FUNCTION__ - too annoying
  91. #define FUNCTION_STR __FUNCTION__
  92. #else
  93. #define FUNCTION_STR __FUNCTION__
  94. #endif
  95. // (*): See https://stackoverflow.com/questions/257418/do-while-0-what-is-it-good-for
  96. #define ERR_FAIL_INDEX(m_index, m_size) \
  97. do { \
  98. if ((m_index) < 0 || (m_index) >= (m_size)) { \
  99. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Index " _STR(m_index) " out of size (" _STR(m_size) ")."); \
  100. return; \
  101. } else \
  102. _err_error_exists = false; \
  103. } while (0); // (*)
  104. /** An index has failed if m_index<0 or m_index >=m_size, the function exists.
  105. * This function returns an error value, if returning Error, please select the most
  106. * appropriate error condition from error_macros.h
  107. */
  108. #define ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
  109. do { \
  110. if ((m_index) < 0 || (m_index) >= (m_size)) { \
  111. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Index " _STR(m_index) " out of size (" _STR(m_size) ")."); \
  112. return m_retval; \
  113. } else \
  114. _err_error_exists = false; \
  115. } while (0); // (*)
  116. /** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
  117. * We'll do UB by returning a null reference and pray that we wont't crash.
  118. */
  119. #define PRAY_BAD_INDEX(m_index, m_size, m_type) \
  120. do { \
  121. if ((m_index) < 0 || (m_index) >= (m_size)) { \
  122. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "SEVERE: Index " _STR(m_index) " out of size (" _STR(m_size) ")."); \
  123. m_type *n = (m_type *)0; /* two-step to avoid warning */ \
  124. return *n; \
  125. } else \
  126. _err_error_exists = false; \
  127. } while (0); // (*)
  128. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  129. * the function will exit.
  130. */
  131. #define ERR_FAIL_NULL(m_param) \
  132. { \
  133. if (!m_param) { \
  134. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \
  135. return; \
  136. } else \
  137. _err_error_exists = false; \
  138. }
  139. #define ERR_FAIL_NULL_V(m_param, m_retval) \
  140. { \
  141. if (!m_param) { \
  142. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Parameter ' " _STR(m_param) " ' is null."); \
  143. return m_retval; \
  144. } else \
  145. _err_error_exists = false; \
  146. }
  147. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  148. * the function will exit.
  149. */
  150. #define ERR_FAIL_COND(m_cond) \
  151. { \
  152. if (m_cond) { \
  153. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true."); \
  154. return; \
  155. } else \
  156. _err_error_exists = false; \
  157. }
  158. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  159. * the function will exit.
  160. * This function returns an error value, if returning Error, please select the most
  161. * appropriate error condition from error_macros.h
  162. */
  163. #define ERR_FAIL_COND_V(m_cond, m_retval) \
  164. { \
  165. if (m_cond) { \
  166. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. returned: " _STR(m_retval)); \
  167. return m_retval; \
  168. } else \
  169. _err_error_exists = false; \
  170. }
  171. /** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
  172. * We'll do UB by returning a null reference and pray that we wont't crash.
  173. */
  174. #define PRAY_COND(m_cond, m_type) \
  175. { \
  176. if (m_cond) { \
  177. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "SEVERE: Condition ' " _STR(m_cond) " ' is true."); \
  178. m_type *n = (m_type *)0; /* two-step to avoid warning */ \
  179. return *n; \
  180. } else \
  181. _err_error_exists = false; \
  182. }
  183. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  184. * the loop will skip to the next iteration.
  185. */
  186. #define ERR_CONTINUE(m_cond) \
  187. { \
  188. if (m_cond) { \
  189. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Continuing..:"); \
  190. continue; \
  191. } else \
  192. _err_error_exists = false; \
  193. }
  194. /** An error condition happened (m_cond tested true) (WARNING this is the opposite as assert().
  195. * the loop will break
  196. */
  197. #define ERR_BREAK(m_cond) \
  198. { \
  199. if (m_cond) { \
  200. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  201. break; \
  202. } else \
  203. _err_error_exists = false; \
  204. }
  205. /** Print an error string and return
  206. */
  207. #define ERR_FAIL() \
  208. { \
  209. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/Function Failed."); \
  210. _err_error_exists = false; \
  211. return; \
  212. }
  213. /** Print an error string and return with value
  214. */
  215. #define ERR_FAIL_V(m_value) \
  216. { \
  217. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Method/Function Failed, returning: " __STR(m_value)); \
  218. _err_error_exists = false; \
  219. return m_value; \
  220. }
  221. /** Use this one if there is no sensible fallback, that is, the error is unrecoverable.
  222. * We'll do UB by returning a null reference and pray that we wont't crash.
  223. */
  224. #define PRAY(m_type) \
  225. { \
  226. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "SEVERE: Method/Function Failed."); \
  227. m_type *n = (m_type *)0; /* two-step to avoid warning */ \
  228. return *n; \
  229. }
  230. /** Print an error string.
  231. */
  232. #define ERR_PRINT(m_string) \
  233. { \
  234. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string); \
  235. _err_error_exists = false; \
  236. }
  237. #define ERR_PRINTS(m_string) \
  238. { \
  239. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, String(m_string).utf8().get_data()); \
  240. _err_error_exists = false; \
  241. }
  242. /** Print a warning string.
  243. */
  244. #define WARN_PRINT(m_string) \
  245. { \
  246. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, m_string, ERR_HANDLER_WARNING); \
  247. _err_error_exists = false; \
  248. }
  249. #define WARN_PRINTS(m_string) \
  250. { \
  251. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, String(m_string).utf8().get_data(), ERR_HANDLER_WARNING); \
  252. _err_error_exists = false; \
  253. }
  254. #endif