Define a user-defined error structure

The calling code can define a user-defined error structure to hold error information. This user-defined structure can be a single C variable or a structure with several pieces of error information. It can contain as much error information as the calling code requires.

The following code fragment shows a sample user-defined error structure named DB_ERROR_BUF.
Figure 1: A sample user-defined error structure
#define MSG_SIZE 256
typedef struct error_buf_
{
   mi_integer error_type;
   mi_integer error_level;
   mi_string sqlstate[6];
   mi_string error_msg[MSG_SIZE];
} DB_ERROR_BUF;
The DB_ERROR_BUF structure holds the following error information.
error_type
The event type for the event

For exception handling, this event type must always be MI_Exception.

error_level
The exception level (or error level) for the event

For exception handling, this field holds the exception level: MI_MESSAGE or MI_EXCEPTION.

sqlstate
The value of the SQLSTATE variable, which indicates the cause of the exception
error_msg
The text of the error message, up to a limit of MSG_SIZE bytes

The calling code must allocate memory for the user-defined error structure. You can use the DataBlade® API memory-allocation functions such as mi_alloc() and mi_dalloc(). When you allocate the user-defined error structure, you must associate a memory duration with this structure that you declare that is appropriate to its usage. For example, if the user-defined error structure is to be associated with a registered callback, you must allocate the structure with a memory duration of PER_STMT_EXEC so that this memory is still allocated when the callback executes.

The following mi_dalloc() call allocates a DB_ERROR_BUF buffer with a PER_STMT_EXEC memory duration:
mi_dalloc(sizeof(DB_ERROR_BUF), PER_STMT_EXEC);