GDAL
cpl_error.h
Go to the documentation of this file.
1/**********************************************************************
2 *
3 * Name: cpl_error.h
4 * Project: CPL - Common Portability Library
5 * Purpose: CPL Error handling
6 * Author: Daniel Morissette, danmo@videotron.ca
7 *
8 **********************************************************************
9 * Copyright (c) 1998, Daniel Morissette
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#ifndef CPL_ERROR_H_INCLUDED
15#define CPL_ERROR_H_INCLUDED
16
17#include "cpl_port.h"
18
19#include <stdarg.h>
20#include <stdbool.h>
21#include <stddef.h>
22
23/*=====================================================================
24 Error handling functions (cpl_error.c)
25 =====================================================================*/
26
38
44typedef enum
45{
48
51
56
61
65 CE_Fatal = 4
67
68/* ==================================================================== */
69/* Well known error codes. */
70/* ==================================================================== */
71
72#ifdef STRICT_CPLERRORNUM_TYPE
73
74/* This is not appropriate for the general case, as there are parts */
75/* of GDAL which use custom error codes, but this can help diagnose confusions
76 */
77/* between CPLErr and CPLErrorNum */
78typedef enum
79{
99
100#else
101
103typedef int CPLErrorNum;
104
106#define CPLE_None 0
108#define CPLE_AppDefined 1
110#define CPLE_OutOfMemory 2
112#define CPLE_FileIO 3
114#define CPLE_OpenFailed 4
116#define CPLE_IllegalArg 5
118#define CPLE_NotSupported 6
120#define CPLE_AssertionFailed 7
122#define CPLE_NoWriteAccess 8
124#define CPLE_UserInterrupt 9
126#define CPLE_ObjectNull 10
127
128/*
129 * Filesystem-specific errors
130 */
132#define CPLE_HttpResponse 11
134#define CPLE_BucketNotFound 12
136#define CPLE_ObjectNotFound 13
138#define CPLE_AccessDenied 14
140#define CPLE_InvalidCredentials 15
142#define CPLE_SignatureDoesNotMatch 16
144#define CPLE_ObjectStorageGenericError 17
145
146/* 100 - 299 reserved for GDAL */
147
148#endif
149
154#define CPLE_AWSBucketNotFound CPLE_BucketNotFound
155
160#define CPLE_AWSObjectNotFound CPLE_ObjectNotFound
161
166#define CPLE_AWSAccessDenied CPLE_AccessDenied
167
172#define CPLE_AWSInvalidCredentials CPLE_InvalidCredentials
173
178#define CPLE_AWSSignatureDoesNotMatch CPLE_SignatureDoesNotMatch
179
184#define CPLE_AWSError CPLE_ObjectStorageGenericError
185
186void CPL_DLL CPLError(CPLErr eErrClass, CPLErrorNum err_no,
187 CPL_FORMAT_STRING(const char *fmt), ...)
189
190#ifdef GDAL_COMPILATION
191
192const char CPL_DLL *CPLSPrintf(CPL_FORMAT_STRING(const char *fmt), ...)
194
200#define CPLErrorOnce(eErrClass, err_no, ...) \
201 do \
202 { \
203 static bool lbCPLErrorOnce = false; \
204 if (!lbCPLErrorOnce) \
205 { \
206 lbCPLErrorOnce = true; \
207 const char *lCPLErrorMsg = CPLSPrintf(__VA_ARGS__); \
208 const size_t lCPLErrorMsgLen = strlen(lCPLErrorMsg); \
209 const char *lCPLErrorMsgSuffix = \
210 " Further messages of this type will be suppressed."; \
211 if (lCPLErrorMsgLen && lCPLErrorMsg[lCPLErrorMsgLen - 1] == '.') \
212 CPLError((eErrClass), (err_no), "%s%s", lCPLErrorMsg, \
213 lCPLErrorMsgSuffix); \
214 else \
215 CPLError((eErrClass), (err_no), "%s.%s", lCPLErrorMsg, \
216 lCPLErrorMsgSuffix); \
217 } \
218 } while (0)
219#endif
220
221void CPL_DLL CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list);
222void CPL_DLL CPLEmergencyError(const char *) CPL_NO_RETURN;
223void CPL_DLL CPL_STDCALL CPLErrorReset(void);
224CPLErrorNum CPL_DLL CPL_STDCALL CPLGetLastErrorNo(void);
225CPLErr CPL_DLL CPL_STDCALL CPLGetLastErrorType(void);
226const char CPL_DLL *CPL_STDCALL CPLGetLastErrorMsg(void);
227GUInt32 CPL_DLL CPL_STDCALL CPLGetErrorCounter(void);
228void CPL_DLL *CPL_STDCALL CPLGetErrorHandlerUserData(void);
229void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no,
230 const char *pszMsg);
231#if defined(GDAL_COMPILATION) && defined(__cplusplus)
232extern "C++"
233{
234 void CPL_DLL CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no,
235 const char *pszMsg,
236 const GUInt32 *pnErrorCounter);
237}
238#endif
239
240void CPL_DLL CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no,
241 const char *pszMsg);
243void CPL_DLL CPLCleanupErrorMutex(void);
247typedef void(CPL_STDCALL *CPLErrorHandler)(CPLErr, CPLErrorNum, const char *);
248
249void CPL_DLL CPL_STDCALL CPLLoggingErrorHandler(CPLErr, CPLErrorNum,
250 const char *);
251void CPL_DLL CPL_STDCALL CPLDefaultErrorHandler(CPLErr, CPLErrorNum,
252 const char *);
253void CPL_DLL CPL_STDCALL CPLQuietErrorHandler(CPLErr, CPLErrorNum,
254 const char *);
255void CPL_DLL CPL_STDCALL CPLQuietWarningsErrorHandler(CPLErr, CPLErrorNum,
256 const char *);
257void CPL_DLL CPLTurnFailureIntoWarning(int bOn);
258
259CPLErrorHandler CPL_DLL CPLGetErrorHandler(void **ppUserData);
260
263 void *);
264void CPL_DLL CPL_STDCALL CPLPushErrorHandler(CPLErrorHandler);
265void CPL_DLL CPL_STDCALL CPLPushErrorHandlerEx(CPLErrorHandler, void *);
266void CPL_DLL CPL_STDCALL CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug);
267void CPL_DLL CPL_STDCALL CPLPopErrorHandler(void);
268
269#ifdef WITHOUT_CPLDEBUG
270#define CPLDebug(...) \
271 do \
272 { \
273 } while (0) /* Eat all CPLDebug calls. */
274#define CPLDebugProgress(...) \
275 do \
276 { \
277 } while (0) /* Eat all CPLDebugProgress calls. */
278
279#ifdef GDAL_COMPILATION
285#define CPLDebugOnce(...) \
286 do \
287 { \
288 } while (0)
289#endif
290
291#else
292void CPL_DLL CPLDebug(const char *, CPL_FORMAT_STRING(const char *), ...)
294void CPL_DLL CPLDebugProgress(const char *, CPL_FORMAT_STRING(const char *),
295 ...) CPL_PRINT_FUNC_FORMAT(2, 3);
296
297#ifdef GDAL_COMPILATION
303#define CPLDebugOnce(category, ...) \
304 do \
305 { \
306 static bool lbCPLDebugOnce = false; \
307 if (!lbCPLDebugOnce) \
308 { \
309 lbCPLDebugOnce = true; \
310 const char *lCPLDebugMsg = CPLSPrintf(__VA_ARGS__); \
311 const size_t lCPLErrorMsgLen = strlen(lCPLDebugMsg); \
312 const char *lCPLDebugMsgSuffix = \
313 " Further messages of this type will be suppressed."; \
314 if (lCPLErrorMsgLen && lCPLDebugMsg[lCPLErrorMsgLen - 1] == '.') \
315 CPLDebug((category), "%s%s", lCPLDebugMsg, \
316 lCPLDebugMsgSuffix); \
317 else \
318 CPLDebug((category), "%s.%s", lCPLDebugMsg, \
319 lCPLDebugMsgSuffix); \
320 } \
321 } while (0)
322#endif
323
324#endif
325
326#if defined(DEBUG) || defined(GDAL_DEBUG)
330#define CPLDebugOnly(...) CPLDebug(__VA_ARGS__)
331#else
335#define CPLDebugOnly(...) \
336 do \
337 { \
338 } while (0)
339#endif
340
341void CPL_DLL CPL_STDCALL _CPLAssert(const char *, const char *,
342 int) CPL_NO_RETURN;
343
344#if defined(DEBUG) && !defined(CPPCHECK)
346#define CPLAssert(expr) \
347 ((expr) ? (void)(0) : _CPLAssert(#expr, __FILE__, __LINE__))
350#define CPLAssertAlwaysEval(expr) CPLAssert(expr)
351#else
353#define CPLAssert(expr) \
354 do \
355 { \
356 } while (0)
357#ifdef __cplusplus
360#define CPLAssertAlwaysEval(expr) CPL_IGNORE_RET_VAL(expr)
361#else
364#define CPLAssertAlwaysEval(expr) (void)(expr)
365#endif
366#endif
367
369
371/*
372 * Helper macros used for input parameters validation.
373 */
374#ifdef DEBUG
375#define VALIDATE_POINTER_ERR CE_Fatal
376#else
377#define VALIDATE_POINTER_ERR CE_Failure
378#endif
379
382#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
383
384extern "C++"
385{
387 template <class T> T *CPLAssertNotNull(T *x) CPL_RETURNS_NONNULL;
388
389 template <class T> T *CPLAssertNotNull(T *x)
390 {
391 CPLAssert(x);
392 return x;
393 }
394
395#include <memory>
396#include <string>
397
404 {
405 public:
410 {
411 CPLPushErrorHandler(hHandler);
412 }
413
417 CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data)
418 {
419 CPLPushErrorHandlerEx(hHandler, user_data);
420 }
421
424 {
425 CPLPopErrorHandler();
426 }
427 };
428
433 {
434 CPLErrorNum m_nLastErrorNum;
435 CPLErr m_nLastErrorType;
436 std::string m_osLastErrorMsg;
437 GUInt32 m_nLastErrorCounter;
438 std::unique_ptr<CPLErrorHandlerPusher> m_poErrorHandlerPusher;
439
440 public:
444 explicit CPLErrorStateBackuper(CPLErrorHandler hHandler = nullptr);
445
450 };
451
456 {
457 public:
459 {
460 CPLTurnFailureIntoWarning(true);
461 }
462
464 {
465 CPLTurnFailureIntoWarning(false);
466 }
467 };
468}
469
470#ifdef GDAL_COMPILATION
472// internal only
473bool CPLIsDefaultErrorHandlerAndCatchDebug();
475#endif
476
477#endif
478
480#define VALIDATE_POINTER0(ptr, func) \
481 do \
482 { \
483 if (CPL_NULLPTR == ptr) \
484 { \
485 CPLErr const ret = VALIDATE_POINTER_ERR; \
486 CPLError(ret, CPLE_ObjectNull, \
487 "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \
488 return; \
489 } \
490 } while (0)
491
493#define VALIDATE_POINTER1(ptr, func, rc) \
494 do \
495 { \
496 if (CPL_NULLPTR == ptr) \
497 { \
498 CPLErr const ret = VALIDATE_POINTER_ERR; \
499 CPLError(ret, CPLE_ObjectNull, \
500 "Pointer \'%s\' is NULL in \'%s\'.\n", #ptr, (func)); \
501 return (rc); \
502 } \
503 } while (0)
504
505#endif /* CPL_ERROR_H_INCLUDED */
Class that installs a (thread-local) error handler on construction, and restore the initial one on de...
Definition cpl_error.h:404
CPLErrorHandlerPusher(CPLErrorHandler hHandler)
Constructor that installs a thread-local temporary error handler (typically CPLQuietErrorHandler)
Definition cpl_error.h:409
CPLErrorHandlerPusher(CPLErrorHandler hHandler, void *user_data)
Constructor that installs a thread-local temporary error handler, and its user data.
Definition cpl_error.h:417
~CPLErrorHandlerPusher()
Destructor that restores the initial error handler.
Definition cpl_error.h:423
Class that saves the error state on construction, and restores it on destruction.
Definition cpl_error.h:433
Class that turns errors into warning on construction, and restores the previous state on destruction.
Definition cpl_error.h:456
void CPLQuietWarningsErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that ignores CE_Warning messages.
Definition cpl_error.cpp:1126
#define CPLE_IllegalArg
Illegal argument.
Definition cpl_error.h:116
void CPLErrorSetState(CPLErr eErrClass, CPLErrorNum err_no, const char *pszMsg)
Restore an error state, without emitting an error.
Definition cpl_error.cpp:890
GUInt32 CPLGetErrorCounter(void)
Get the error counter.
Definition cpl_error.cpp:981
#define CPLE_AssertionFailed
Assertion failed.
Definition cpl_error.h:120
void CPLPushErrorHandlerEx(CPLErrorHandler, void *)
Push a new CPLError handler with user data on the error context.
Definition cpl_error.cpp:1364
#define CPLE_None
No error.
Definition cpl_error.h:106
#define CPLE_ObjectNotFound
VSIE_ObjectNotFound.
Definition cpl_error.h:136
void CPLErrorReset(void)
Erase any traces of previous errors.
Definition cpl_error.cpp:815
void CPLLoggingErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that logs into the file defined by the CPL_LOG configuration option,...
Definition cpl_error.cpp:1142
#define CPLE_NotSupported
Not supported.
Definition cpl_error.h:118
#define CPLAssert(expr)
Assert on an expression.
Definition cpl_error.h:353
CPLErr
Error category / error level.
Definition cpl_error.h:45
@ CE_Warning
Non-nominal situation that is worth bringing to the attention of the user, but that does not prevent ...
Definition cpl_error.h:55
@ CE_Debug
Debug message.
Definition cpl_error.h:50
@ CE_None
No error.
Definition cpl_error.h:47
@ CE_Fatal
Fatal unrecoverable error.
Definition cpl_error.h:65
@ CE_Failure
Error that prevents the current operation to succeed.
Definition cpl_error.h:60
#define CPLE_FileIO
File I/O error.
Definition cpl_error.h:112
CPLErrorNum CPLGetLastErrorNo(void)
Fetch the last error number.
Definition cpl_error.cpp:911
void CPLDebugProgress(const char *, const char *,...)
Display a debugging message indicating a progression.
Definition cpl_error.cpp:785
#define CPLE_AppDefined
Application defined error.
Definition cpl_error.h:108
#define CPLE_OpenFailed
Open failed.
Definition cpl_error.h:114
const char * CPLGetLastErrorMsg(void)
Get the last error message.
Definition cpl_error.cpp:959
CPLErrorHandler CPLGetErrorHandler(void **ppUserData)
Fetch the current error handler for the current error context.
Definition cpl_error.cpp:175
CPLErrorHandler CPLSetErrorHandler(CPLErrorHandler)
Install custom error handler.
Definition cpl_error.cpp:1320
#define CPLE_AccessDenied
VSIE_AccessDenied.
Definition cpl_error.h:138
#define CPLE_ObjectNull
NULL object.
Definition cpl_error.h:126
void CPLDefaultErrorHandler(CPLErr, CPLErrorNum, const char *)
Default error handler.
Definition cpl_error.cpp:1015
CPLErr CPLGetLastErrorType(void)
Fetch the last error type.
Definition cpl_error.cpp:935
int CPLErrorNum
Error number.
Definition cpl_error.h:103
void * CPLGetErrorHandlerUserData(void)
Fetch the user data for the error context.
Definition cpl_error.cpp:147
void(* CPLErrorHandler)(CPLErr, CPLErrorNum, const char *)
Callback for a custom error handler.
Definition cpl_error.h:247
void CPLTurnFailureIntoWarning(int bOn)
Whether failures should be turned into warnings.
Definition cpl_error.cpp:1215
void CPLCallPreviousHandler(CPLErr eErrClass, CPLErrorNum err_no, const char *pszMsg)
Call the previously installed error handler in the error handler stack.
Definition cpl_error.cpp:1431
#define CPLE_InvalidCredentials
VSIE_InvalidCredentials.
Definition cpl_error.h:140
void CPLEmergencyError(const char *)
Fatal error when things are bad.
Definition cpl_error.cpp:496
#define CPLE_SignatureDoesNotMatch
VSIE_SignatureDoesNotMatch.
Definition cpl_error.h:142
void _CPLAssert(const char *, const char *, int)
Report failure of a logical assertion.
Definition cpl_error.cpp:1519
#define CPLE_HttpResponse
HTTP response.
Definition cpl_error.h:132
void CPLPopErrorHandler(void)
Pop error handler off stack.
Definition cpl_error.cpp:1398
#define CPLE_BucketNotFound
VSIE_BucketNotFound.
Definition cpl_error.h:134
void CPLDebug(const char *, const char *,...)
Display a debugging message.
Definition cpl_error.cpp:740
void CPLPushErrorHandler(CPLErrorHandler)
Push a new CPLError handler.
Definition cpl_error.cpp:1341
#define CPLE_NoWriteAccess
No write access.
Definition cpl_error.h:122
#define CPLE_ObjectStorageGenericError
VSIE_ObjectStorageGenericError.
Definition cpl_error.h:144
#define CPLE_UserInterrupt
User interrupted.
Definition cpl_error.h:124
void CPLErrorV(CPLErr, CPLErrorNum, const char *, va_list)
Same as CPLError() but with a va_list.
Definition cpl_error.cpp:332
void CPLQuietErrorHandler(CPLErr, CPLErrorNum, const char *)
Error handler that does not do anything, except for debug messages.
Definition cpl_error.cpp:1113
#define CPLE_OutOfMemory
Out of memory error.
Definition cpl_error.h:110
CPLErrorHandler CPLSetErrorHandlerEx(CPLErrorHandler, void *)
Install custom error handle with user's data.
Definition cpl_error.cpp:1247
void CPLSetCurrentErrorHandlerCatchDebug(int bCatchDebug)
Set if the current error handler should intercept debug messages, or if they should be processed by t...
Definition cpl_error.cpp:1485
void CPLError(CPLErr eErrClass, CPLErrorNum err_no, const char *fmt,...)
Report an error.
Definition cpl_error.cpp:316
Core portability definitions for CPL.
#define CPL_NO_RETURN
Qualifier for a function that does not return at all (terminates the process)
Definition cpl_port.h:1054
#define CPL_C_END
Macro to end a block of C symbols.
Definition cpl_port.h:279
#define CPL_C_START
Macro to start a block of C symbols.
Definition cpl_port.h:275
#define CPL_FORMAT_STRING(arg)
Macro into which to wrap the format argument of a printf-like function.
Definition cpl_port.h:1025
#define CPL_RETURNS_NONNULL
Qualifier for a function that does not return NULL.
Definition cpl_port.h:1073
unsigned int GUInt32
Unsigned int32 type.
Definition cpl_port.h:157
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition cpl_port.h:1009
#define CPL_WARN_UNUSED_RESULT
Qualifier to warn when the return value of a function is not used.
Definition cpl_port.h:1035