Error Tutorial
Registering messages
The error module can provide a system that mimics the errno system of the C standard library. It consists in 2 parts:
- a way of registering new messages with eina_error_msg_register() and eina_error_msg_get(),
- a way of setting / getting last error message with eina_error_set() / eina_error_get().
So one has to fisrt register all the error messages that a program or a lib should manage. Then, when an error can occur, use eina_error_set(), and when errors are managed, use eina_error_get(). If eina_error_set() is used to set an error, do not forget to call before eina_error_set(), to remove previous set errors.
Here is an example of use:
//Compile with:
//gcc -g eina_error_01.c -o eina_error_01 `pkg-config --cflags --libs eina`
#include <stdlib.h>
#include <stdio.h>
#include <eina_error.h>
Eina_Error MY_ERROR_NEGATIVE;
Eina_Error MY_ERROR_NULL;
void *data_new()
{
eina_error_set(0);
eina_error_set(MY_ERROR_NULL);
return NULL;
}
int test(int n)
{
eina_error_set(0);
if (n < 0)
{
eina_error_set(MY_ERROR_NEGATIVE);
return 0;
}
return 1;
}
int main(void)
{
void *data;
if (!eina_init())
{
printf ("Error during the initialization of eina_error module\n");
return EXIT_FAILURE;
}
MY_ERROR_NEGATIVE = eina_error_msg_static_register("Negative number");
MY_ERROR_NULL = eina_error_msg_static_register("NULL pointer");
data = data_new();
if (!data)
{
Eina_Error err;
err = eina_error_get();
if (err)
printf("Error during memory allocation: %s\n",
eina_error_msg_get(err));
}
if (!test(0))
{
Eina_Error err;
err = eina_error_get();
if (err)
printf("Error during test function: %s\n",
eina_error_msg_get(err));
}
if (!test(-1))
{
Eina_Error err;
err = eina_error_get();
if (err)
printf("Error during test function: %s\n",
eina_error_msg_get(err));
}
return EXIT_SUCCESS;
}
Of course, instead of printf(), eina_log_print() can be used to have beautiful error messages.