\hypertarget{tutorial_error_page_tutorial_error_registering_msg}{}\section{Registering messages}\label{tutorial_error_page_tutorial_error_registering_msg}
The error module can provide a system that mimics the errno system of the C standard library. It consists in 2 parts\+:

\begin{DoxyItemize}
\item A way of registering new messages with \hyperlink{group__Eina__Error__Group_gacd6991f1ce57f853ddb19f32bb4aad38}{eina\+\_\+error\+\_\+msg\+\_\+register()} and \hyperlink{group__Eina__Error__Group_ga3402bc58a9f854391eb35fa7650edda5}{eina\+\_\+error\+\_\+msg\+\_\+get()}, \item A way of setting / getting the last error message with \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\+\_\+error\+\_\+set()} / \hyperlink{group__Eina__Error__Group_gae0683831c4f8eebfdcb4f3cf1aef7052}{eina\+\_\+error\+\_\+get()}.\end{DoxyItemize}
So one has to first register all the error messages that a program or a library should manage. Then, when an error occurs, use \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\+\_\+error\+\_\+set()}, and when errors are managed, use \hyperlink{group__Eina__Error__Group_gae0683831c4f8eebfdcb4f3cf1aef7052}{eina\+\_\+error\+\_\+get()}. If \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\+\_\+error\+\_\+set()} is used to set an error, do not forget to remove previous set errors before calling \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\+\_\+error\+\_\+set()}.

Here is an example for use\+:


\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -g eina\_error\_01.c -o eina\_error\_01 `pkg-config --cflags --libs eina`}

\textcolor{preprocessor}{#include <stdlib.h>}
\textcolor{preprocessor}{#include <stdio.h>}

\textcolor{preprocessor}{#include <eina\_main.h>}
\textcolor{preprocessor}{#include <eina\_error.h>}

\hyperlink{group__Eina__Error__Group_ga95b3cbb33e987e7d92a81ea785001686}{Eina\_Error} MY\_ERROR\_NEGATIVE;
\hyperlink{group__Eina__Error__Group_ga95b3cbb33e987e7d92a81ea785001686}{Eina\_Error} MY\_ERROR\_NULL;

\textcolor{keywordtype}{void} *data\_new()
\{
   \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\_error\_set}(0);

   \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\_error\_set}(MY\_ERROR\_NULL);
   \textcolor{keywordflow}{return} NULL;
\}

\textcolor{keywordtype}{int} test(\textcolor{keywordtype}{int} n)
\{
   \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\_error\_set}(0);

   \textcolor{keywordflow}{if} (n < 0)
     \{
        \hyperlink{group__Eina__Error__Group_ga80a8249f135460d4e07ebfe3f0a7bad1}{eina\_error\_set}(MY\_ERROR\_NEGATIVE);
        \textcolor{keywordflow}{return} 0;
     \}

   \textcolor{keywordflow}{return} 1;
\}

\textcolor{keywordtype}{int} main(\textcolor{keywordtype}{void})
\{
   \textcolor{keywordtype}{void} *data;

   \textcolor{keywordflow}{if} (!\hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}())
     \{
        printf (\textcolor{stringliteral}{"Error during the initialization of eina\_error module\(\backslash\)n"});
        \textcolor{keywordflow}{return} EXIT\_FAILURE;
     \}

   MY\_ERROR\_NEGATIVE = \hyperlink{group__Eina__Error__Group_ga004c7ceb7103cd5a698247ae3eb4578a}{eina\_error\_msg\_static\_register}(\textcolor{stringliteral}{"Negative number"});
   MY\_ERROR\_NULL = \hyperlink{group__Eina__Error__Group_ga004c7ceb7103cd5a698247ae3eb4578a}{eina\_error\_msg\_static\_register}(\textcolor{stringliteral}{"NULL pointer"});

   data = data\_new();
   \textcolor{keywordflow}{if} (!data)
     \{
        \hyperlink{group__Eina__Error__Group_ga95b3cbb33e987e7d92a81ea785001686}{Eina\_Error} err;

        err = \hyperlink{group__Eina__Error__Group_gae0683831c4f8eebfdcb4f3cf1aef7052}{eina\_error\_get}();
        \textcolor{keywordflow}{if} (err)
           printf(\textcolor{stringliteral}{"Error during memory allocation: %s\(\backslash\)n"},
                  \hyperlink{group__Eina__Error__Group_ga3402bc58a9f854391eb35fa7650edda5}{eina\_error\_msg\_get}(err));
     \}

   \textcolor{keywordflow}{if} (!test(0))
     \{
        \hyperlink{group__Eina__Error__Group_ga95b3cbb33e987e7d92a81ea785001686}{Eina\_Error} err;

        err = \hyperlink{group__Eina__Error__Group_gae0683831c4f8eebfdcb4f3cf1aef7052}{eina\_error\_get}();
        \textcolor{keywordflow}{if} (err)
           printf(\textcolor{stringliteral}{"Error during test function: %s\(\backslash\)n"},
                  \hyperlink{group__Eina__Error__Group_ga3402bc58a9f854391eb35fa7650edda5}{eina\_error\_msg\_get}(err));
     \}

   \textcolor{keywordflow}{if} (!test(-1))
     \{
        \hyperlink{group__Eina__Error__Group_ga95b3cbb33e987e7d92a81ea785001686}{Eina\_Error} err;

        err = \hyperlink{group__Eina__Error__Group_gae0683831c4f8eebfdcb4f3cf1aef7052}{eina\_error\_get}();
        \textcolor{keywordflow}{if} (err)
           printf(\textcolor{stringliteral}{"Error during test function: %s\(\backslash\)n"},
                  \hyperlink{group__Eina__Error__Group_ga3402bc58a9f854391eb35fa7650edda5}{eina\_error\_msg\_get}(err));
     \}

   \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();

   \textcolor{keywordflow}{return} EXIT\_SUCCESS;
\}
\end{DoxyCodeInclude}


Of course, instead of printf(), \hyperlink{group__Eina__Log__Group_ga4cca81b7002645e5550283f687b903db}{eina\+\_\+log\+\_\+print()} can be used to have beautiful error messages. 