feature: add error conversion functions for DRANG error codes

This commit is contained in:
MechSlayer 2025-09-05 21:40:27 +02:00
parent eb60ab304a
commit 3211bb45b3
3 changed files with 68 additions and 0 deletions

View file

@ -21,6 +21,7 @@ DRANG_PLATFORM_API const char *drang_error_str(int err);
#define DRANG_EPERM (-8) // Operation not permitted
#define DRANG_ETIMEDOUT (-9) // Connection timed out
#define DRANG_EBUSY (-10) // Device or resource busy
#define DRANG_EPLATFORM (-1000) // An unknown platform-specific error occurred
/**
* @brief Begins a try-catch block for error handling.

View file

@ -0,0 +1,56 @@
#include "errno_convert.h"
#include "drang/error.h"
#include <errno.h>
int drang_error_to_errno(int error)
{
switch (error) {
case DRANG_EOK:
return 0;
case DRANG_EAGAIN:
return EAGAIN;
case DRANG_ENOMEM:
return ENOMEM;
case DRANG_EINVAL:
return EINVAL;
case DRANG_ENOENT:
return ENOENT;
case DRANG_EDEADLK:
return EDEADLK;
case DRANG_EPERM:
return EPERM;
case DRANG_ETIMEDOUT:
return ETIMEDOUT;
case DRANG_EBUSY:
return EBUSY;
case DRANG_EPLATFORM:
default:
return EIO; // Generic I/O error for unknown platform errors
}
}
int drang_errno_to_error(const int errnum)
{
switch (errnum) {
case 0:
return DRANG_EOK;
case EAGAIN:
return DRANG_EAGAIN;
case ENOMEM:
return DRANG_ENOMEM;
case EINVAL:
return DRANG_EINVAL;
case ENOENT:
return DRANG_ENOENT;
case EDEADLK:
return DRANG_EDEADLK;
case EPERM:
return DRANG_EPERM;
case ETIMEDOUT:
return DRANG_ETIMEDOUT;
case EBUSY:
return DRANG_EBUSY;
default:
return DRANG_EPLATFORM;
}
}

View file

@ -0,0 +1,11 @@
#pragma once
#include <drang/platform.h>
DRANG_BEGIN_DECLS
int drang_error_to_errno(int error);
int drang_errno_to_error(int errnum);
DRANG_END_DECLS