SDL_error.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryError
  20. *
  21. * Simple error message routines for SDL.
  22. *
  23. * Most apps will interface with these APIs in exactly one function: when
  24. * almost any SDL function call reports failure, you can get a human-readable
  25. * string of the problem from SDL_GetError().
  26. *
  27. * These strings are maintained per-thread, and apps are welcome to set their
  28. * own errors, which is popular when building libraries on top of SDL for
  29. * other apps to consume. These strings are set by calling SDL_SetError().
  30. *
  31. * A common usage pattern is to have a function that returns true for success
  32. * and false for failure, and do this when something fails:
  33. *
  34. * ```c
  35. * if (something_went_wrong) {
  36. * return SDL_SetError("The thing broke in this specific way: %d", errcode);
  37. * }
  38. * ```
  39. *
  40. * It's also common to just return `false` in this case if the failing thing
  41. * is known to call SDL_SetError(), so errors simply propagate through.
  42. */
  43. #ifndef SDL_error_h_
  44. #define SDL_error_h_
  45. #include <SDL3/SDL_stdinc.h>
  46. #include <SDL3/SDL_begin_code.h>
  47. /* Set up for C function definitions, even when using C++ */
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /* Public functions */
  52. /**
  53. * Set the SDL error message for the current thread.
  54. *
  55. * Calling this function will replace any previous error message that was set.
  56. *
  57. * This function always returns false, since SDL frequently uses false to
  58. * signify a failing result, leading to this idiom:
  59. *
  60. * ```c
  61. * if (error_code) {
  62. * return SDL_SetError("This operation has failed: %d", error_code);
  63. * }
  64. * ```
  65. *
  66. * \param fmt a printf()-style message format string.
  67. * \param ... additional parameters matching % tokens in the `fmt` string, if
  68. * any.
  69. * \returns false.
  70. *
  71. * \threadsafety It is safe to call this function from any thread.
  72. *
  73. * \since This function is available since SDL 3.1.3.
  74. *
  75. * \sa SDL_ClearError
  76. * \sa SDL_GetError
  77. * \sa SDL_SetErrorV
  78. */
  79. extern SDL_DECLSPEC bool SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
  80. /**
  81. * Set the SDL error message for the current thread.
  82. *
  83. * Calling this function will replace any previous error message that was set.
  84. *
  85. * \param fmt a printf()-style message format string.
  86. * \param ap a variable argument list.
  87. * \returns false.
  88. *
  89. * \threadsafety It is safe to call this function from any thread.
  90. *
  91. * \since This function is available since SDL 3.1.6.
  92. *
  93. * \sa SDL_ClearError
  94. * \sa SDL_GetError
  95. * \sa SDL_SetError
  96. */
  97. extern SDL_DECLSPEC bool SDLCALL SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(1);
  98. /**
  99. * Set an error indicating that memory allocation failed.
  100. *
  101. * This function does not do any memory allocation.
  102. *
  103. * \returns false.
  104. *
  105. * \threadsafety It is safe to call this function from any thread.
  106. *
  107. * \since This function is available since SDL 3.1.3.
  108. */
  109. extern SDL_DECLSPEC bool SDLCALL SDL_OutOfMemory(void);
  110. /**
  111. * Retrieve a message about the last error that occurred on the current
  112. * thread.
  113. *
  114. * It is possible for multiple errors to occur before calling SDL_GetError().
  115. * Only the last error is returned.
  116. *
  117. * The message is only applicable when an SDL function has signaled an error.
  118. * You must check the return values of SDL function calls to determine when to
  119. * appropriately call SDL_GetError(). You should *not* use the results of
  120. * SDL_GetError() to decide if an error has occurred! Sometimes SDL will set
  121. * an error string even when reporting success.
  122. *
  123. * SDL will *not* clear the error string for successful API calls. You *must*
  124. * check return values for failure cases before you can assume the error
  125. * string applies.
  126. *
  127. * Error strings are set per-thread, so an error set in a different thread
  128. * will not interfere with the current thread's operation.
  129. *
  130. * The returned value is a thread-local string which will remain valid until
  131. * the current thread's error string is changed. The caller should make a copy
  132. * if the value is needed after the next SDL API call.
  133. *
  134. * \returns a message with information about the specific error that occurred,
  135. * or an empty string if there hasn't been an error message set since
  136. * the last call to SDL_ClearError().
  137. *
  138. * \threadsafety It is safe to call this function from any thread.
  139. *
  140. * \since This function is available since SDL 3.1.3.
  141. *
  142. * \sa SDL_ClearError
  143. * \sa SDL_SetError
  144. */
  145. extern SDL_DECLSPEC const char * SDLCALL SDL_GetError(void);
  146. /**
  147. * Clear any previous error message for this thread.
  148. *
  149. * \returns true.
  150. *
  151. * \threadsafety It is safe to call this function from any thread.
  152. *
  153. * \since This function is available since SDL 3.1.3.
  154. *
  155. * \sa SDL_GetError
  156. * \sa SDL_SetError
  157. */
  158. extern SDL_DECLSPEC bool SDLCALL SDL_ClearError(void);
  159. /**
  160. * \name Internal error functions
  161. *
  162. * \internal
  163. * Private error reporting function - used internally.
  164. */
  165. /* @{ */
  166. /**
  167. * A macro to standardize error reporting on unsupported operations.
  168. *
  169. * This simply calls SDL_SetError() with a standardized error string, for
  170. * convenience, consistency, and clarity.
  171. *
  172. * \threadsafety It is safe to call this macro from any thread.
  173. *
  174. * \since This macro is available since SDL 3.1.3.
  175. */
  176. #define SDL_Unsupported() SDL_SetError("That operation is not supported")
  177. /**
  178. * A macro to standardize error reporting on unsupported operations.
  179. *
  180. * This simply calls SDL_SetError() with a standardized error string, for
  181. * convenience, consistency, and clarity.
  182. *
  183. * A common usage pattern inside SDL is this:
  184. *
  185. * ```c
  186. * bool MyFunction(const char *str) {
  187. * if (!str) {
  188. * return SDL_InvalidParamError("str"); // returns false.
  189. * }
  190. * DoSomething(str);
  191. * return true;
  192. * }
  193. * ```
  194. *
  195. * \threadsafety It is safe to call this macro from any thread.
  196. *
  197. * \since This macro is available since SDL 3.1.3.
  198. */
  199. #define SDL_InvalidParamError(param) SDL_SetError("Parameter '%s' is invalid", (param))
  200. /* @} *//* Internal error functions */
  201. /* Ends C function definitions when using C++ */
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #include <SDL3/SDL_close_code.h>
  206. #endif /* SDL_error_h_ */