1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 /**
23  * # CategoryError
24  *
25  * Simple error message routines for SDL.
26  */
27 
28 #ifndef SDL_error_h_
29 #define SDL_error_h_
30 
31 #include "SDL_stdinc.h"
32 
33 #include "begin_code.h"
34 /* Set up for C function definitions, even when using C++ */
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /* Public functions */
40 
41 
42 /**
43  * Set the SDL error message for the current thread.
44  *
45  * Calling this function will replace any previous error message that was set.
46  *
47  * This function always returns -1, since SDL frequently uses -1 to signify an
48  * failing result, leading to this idiom:
49  *
50  * ```c
51  * if (error_code)
52  {
53  *     return SDL_SetError("This operation has failed: %d", error_code);
54  * }
55  * ```
56  *
57  * \param fmt a printf()-style message format string.
58  * \param ... additional parameters matching % tokens in the `fmt` string, if
59  *            any.
60  * \returns always -1.
61  *
62  * \since This function is available since SDL 2.0.0.
63  *
64  * \sa SDL_ClearError
65  * \sa SDL_GetError
66  */
67 extern DECLSPEC int SDLCALL SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(1);
68 
69 /**
70  * Retrieve a message about the last error that occurred on the current
71  * thread.
72  *
73  * It is possible for multiple errors to occur before calling SDL_GetError().
74  * Only the last error is returned.
75  *
76  * The message is only applicable when an SDL function has signaled an error.
77  * You must check the return values of SDL function calls to determine when to
78  * appropriately call SDL_GetError(). You should *not* use the results of
79  * SDL_GetError() to decide if an error has occurred! Sometimes SDL will set
80  * an error string even when reporting success.
81  *
82  * SDL will *not* clear the error string for successful API calls. You *must*
83  * check return values for failure cases before you can assume the error
84  * string applies.
85  *
86  * Error strings are set per-thread, so an error set in a different thread
87  * will not interfere with the current thread's operation.
88  *
89  * The returned string is internally allocated and must not be freed by the
90  * application.
91  *
92  * \returns a message with information about the specific error that occurred,
93  *          or an empty string if there hasn't been an error message set since
94  *          the last call to SDL_ClearError(). The message is only applicable
95  *          when an SDL function has signaled an error. You must check the
96  *          return values of SDL function calls to determine when to
97  *          appropriately call SDL_GetError().
98  *
99  * \since This function is available since SDL 2.0.0.
100  *
101  * \sa SDL_ClearError
102  * \sa SDL_SetError
103  */
104 extern DECLSPEC const char *SDLCALL SDL_GetError(void);
105 
106 /**
107  * Get the last error message that was set for the current thread.
108  *
109  * This allows the caller to copy the error string into a provided buffer, but
110  * otherwise operates exactly the same as SDL_GetError().
111  *
112  * \param errstr A buffer to fill with the last error message that was set for
113  *               the current thread.
114  * \param maxlen The size of the buffer pointed to by the errstr parameter.
115  * \returns the pointer passed in as the `errstr` parameter.
116  *
117  * \since This function is available since SDL 2.0.14.
118  *
119  * \sa SDL_GetError
120  */
121 extern DECLSPEC char * SDLCALL SDL_GetErrorMsg(char *errstr, int maxlen);
122 
123 /**
124  * Clear any previous error message for this thread.
125  *
126  * \since This function is available since SDL 2.0.0.
127  *
128  * \sa SDL_GetError
129  * \sa SDL_SetError
130  */
131 extern DECLSPEC void SDLCALL SDL_ClearError(void);
132 
133 /**
134  *  \name Internal error functions
135  *
136  *  \internal
137  *  Private error reporting function - used internally.
138  */
139 /* @{ */
140 #define SDL_OutOfMemory()   SDL_Error(SDL_ENOMEM)
141 #define SDL_Unsupported()   SDL_Error(SDL_UNSUPPORTED)
142 #define SDL_InvalidParamError(param)    SDL_SetError("Parameter '%s' is invalid", (param))
143 typedef enum
144 {
145     SDL_ENOMEM,
146     SDL_EFREAD,
147     SDL_EFWRITE,
148     SDL_EFSEEK,
149     SDL_UNSUPPORTED,
150     SDL_LASTERROR
151 } SDL_errorcode;
152 /* SDL_Error() unconditionally returns -1. */
153 extern DECLSPEC int SDLCALL SDL_Error(SDL_errorcode code);
154 /* @} *//* Internal error functions */
155 
156 /* Ends C function definitions when using C++ */
157 #ifdef __cplusplus
158 }
159 #endif
160 #include "close_code.h"
161 
162 #endif /* SDL_error_h_ */
163 
164 /* vi: set ts=4 sw=4 expandtab: */