libikarus/include/ikarus/errors.h
Folling 1ce811d566
implement remaining logic
Signed-off-by: Folling <mail@folling.io>
2025-01-01 14:07:47 +01:00

122 lines
6 KiB
C

#pragma once
/// \file global.h
/// \author Folling <folling@ikarus.world>
#include <ikarus/macros.h>
#include <ikarus/stdtypes.h>
/// \addtogroup errors Errors
/// \brief Error handling within libikarus
/// \details Functions in Ikarus may fail, in which case they have an out parameter for the error.
/// Upon erring the function will store relevant information about the error in the out parameter.
/// If the out parameter is null nothing will be stored. This is not recommended as it essentially ignores errors.
/// For the sake of simplicity we have avoided mechanisms that "force" clients to handle errors.
/// Note that Ikarus does not check for null pointers. Passing null pointers to functions that do not explicitly state that they accept null
/// pointers is undefined behaviour. This decision is done for the sake of brevity and readability. `project_get_name(project)` is also
/// synonymous to `project->get_name()` in OOP languages, which shares the same semantics.
/// @{
IKARUS_BEGIN_HEADER
/// \brief Delineates what caused an error.
/// \remark Note that this doesn't show responsibility. An error with source "SubSystem" could still be the
/// fault of libikarus, it just indicates where the error occurred.
enum IkarusErrorInfo {
/// \brief No error occurred.
IkarusErrorInfo_None = 0x0,
/// \brief The client misused the API.
/// Example: Accessing a resource that does not exist.
IkarusErrorInfo_Client_Misuse = 0x01000001,
/// \brief The client provided a null value for a parameter that must not be null.
/// Example: Passing null for `ikarus_project_get_name`
IkarusErrorInfo_Client_InvalidNull = 0x01000002,
/// \brief The client provided an index that was out of bounds for some array.
/// Example: Passing the index 3 for an `IkarusToggleValue` with size 3.
IkarusErrorInfo_Client_IndexOutOfBounds = 0x01000003,
/// \brief The client provided a numeric value that was out of bounds
/// Example: Passing the value 2^32 to an i32 (might be passed as a string).
IkarusErrorInfo_Client_ValueOutOfBounds = 0x01000004,
/// \brief The client provided invalid input that doesn't fit in any of the other categories.
/// Example: Passing an empty/blank string for a string that must be non-empty/-blank.
IkarusErrorInfo_Client_InvalidInput = 0x01000005,
/// \brief The client provided valid data in an invalid format.
/// Example: Passing a malformed JSON string.
IkarusErrorInfo_Client_InvalidFormat = 0x01000006,
/// \brief The client violated a constraint.
/// \details This error is most likely caused by endusers.
/// Example: A user tries to set the age of a character to an value outside of their specified range.
IkarusErrorInfo_Client_ConstraintViolated = 0x10000007,
// 0x02 reserved for dependency errors
/// \brief A file or directory already exists.
IkarusErrorInfo_Filesystem_AccessIssue = 0x03000001,
/// \brief A file was not found.
IkarusErrorInfo_Filesystem_NotFound = 0x03000002,
/// \brief A file or directory already exists.
IkarusErrorInfo_Filesystem_AlreadyExists = 0x03000003,
/// \brief Missing permissions to access a file or directory.
IkarusErrorInfo_Filesystem_MissingPermissions = 0x03000004,
/// \brief Insufficient space to perform an operation.
IkarusErrorInfo_Filesystem_InsufficientSpace = 0x03000005,
/// \brief A path is invalid.
IkarusErrorInfo_Filesystem_InvalidPath = 0x03000006,
/// \brief A database connection failed.
IkarusErrorInfo_Database_ConnectionFailed = 0x04000001,
/// \brief A database query failed.
IkarusErrorInfo_Database_QueryFailed = 0x04000002,
/// \brief A database migration failed.
IkarusErrorInfo_Database_MigrationFailed = 0x04000003,
/// \brief A database is in an invalid state. This indicates a corrupt project.
/// Example: An entity is linked to a non-existant blueprint.
IkarusErrorInfo_Database_InvalidState = 0x04000004,
/// \brief A system call failed.
IkarusErrorInfo_OS_SystemCallFailed = 0x05000001,
/// \brief A system call returned an invalid value.
IkarusErrorInfo_OS_InvalidReturnValue = 0x05000002,
/// \brief An OOM error occurred.
IkarusErrorInfo_OS_InsufficientMemory = 0x05000003,
/// \brief A datapoint within ikarus is invalid for the current state of the system.
/// \details This differs from IkarusErrorInfo_Database_InvalidState in that the latter implies the database itself holds invalid state,
/// whereas the former may imply that the state is ephemeral, e.g. data within a function.
/// Example: The name of an object is found to be invalid UTF8.
IkarusErrorInfo_LibIkarus_InvalidState = 0x06000001,
/// \brief libikarus is unable to perform a certain operation that should succeed.
IkarusErrorInfo_LibIkarus_CannotPerformOperation = 0x06000002,
/// \brief libikarus is unable to perform a certain operation within a given timeframe.
/// Example: A query takes longer than the timeout.
IkarusErrorInfo_LibIkarus_Timeout = 0x06000003,
};
size_t const IKARUS_ERROR_DATA_MAX_MESSAGE_LIMIT = 128;
/// \brief The data stored for an error
struct IkarusErrorData {
/// \brief The error type
IkarusErrorInfo info;
char message[IKARUS_ERROR_DATA_MAX_MESSAGE_LIMIT];
};
/// \brief Gets the name of an error info.
/// \param info The error info to get the name of.
/// \return The name of the error info.
/// \remark The returned pointer is valid for the lifetime of the program and must not be freed.
IKA_API char const * get_error_info_name(IkarusErrorInfo info);
/// \brief Checks if an error data is a success.
/// \param data The error data to check.
/// \return True if the error data is a success, false otherwise.
IKA_API bool ikarus_error_data_is_success(IkarusErrorData const * data);
/// \brief Checks if an error data is an error.
/// \param data The error data to check.
/// \return True if the error data is an error, false otherwise.
IKA_API bool ikarus_error_data_is_error(IkarusErrorData const * data);
IKARUS_END_HEADER
/// @}