feature: add error handling functions and definitions

This commit is contained in:
MechSlayer 2025-09-05 04:06:07 +02:00
parent d1a39262d8
commit 29e8753d1f
2 changed files with 98 additions and 0 deletions

View file

@ -0,0 +1,83 @@
#pragma once
#include "platform.h"
DRANG_BEGIN_DECLS
/**
* @brief Returns a human-readable string for the given DRANG error code.
* @param err Error code.
* @return String description of the error code.
*/
DRANG_API const char *drang_error_str(int err);
#define DRANG_EOK (0)
#define DRANG_EINVAL (-1)
#define DRANG_ENOMEM (-2)
#define DRANG_EIO (-3)
#define DRANG_NOTSUP (-4)
#define DRANG_EAGAIN (-5)
#define DRANG_ENOENT (-6)
#define DRANG_BEGIN_TRY() \
int _res = DRANG_EOK; \
{
#define DRANG_CATCH(name) \
} \
_drang_cleanup: \
if (_res != DRANG_EOK) { \
int name = _res;
#define DRANG_END_TRY() \
} \
return _res;
#define DRANG_END_TRY_IGNORE() \
} \
_drang_cleanup: \
return _res;
#define DRANG_TRY(expr) \
do { \
_res = (expr); \
if (_res != DRANG_EOK) { \
goto _drang_cleanup; \
} \
} while (0)
#define DRANG_CHECK(expr, err) \
do { \
if (!(expr)) { \
_res = (err); \
goto _drang_cleanup; \
} \
} while (0)
#define DRANG_FAIL(err) \
_res = (err); \
goto _drang_cleanup
#define DRANG_RETURN_IN(_Out_Ptr_, _Value_) \
_res = DRANG_EOK; \
(*(_Out_Ptr_) = (_Value_)); \
goto _drang_cleanup
#define DRANG_RETURN() \
_res = DRANG_EOK; \
goto _drang_cleanup
#define DRANG_FAIL_IF_NULL(ptr, err) \
do { \
if ((ptr) == NULL) { \
_res = (err); \
goto _drang_cleanup; \
} \
} while (0)
#define DRANG_FAIL_IF_NULL_OOM(ptr) \
do { \
if ((ptr) == NULL) { \
_res = DRANG_ENOMEM; \
goto _drang_cleanup; \
} \
} while (0)
DRANG_END_DECLS

View file

@ -0,0 +1,15 @@
#include <drang/error.h>
const char *drang_error_str(const int err)
{
switch (err) {
case DRANG_EOK: return "No error";
case DRANG_EINVAL: return "Invalid argument";
case DRANG_ENOMEM: return "Out of memory";
case DRANG_EIO: return "I/O error";
case DRANG_NOTSUP: return "Operation not supported";
case DRANG_EAGAIN: return "Resource temporarily unavailable";
case DRANG_ENOENT: return "No such file or directory";
default: return "Unknown error";
}
}