Associate a user-defined error structure with the connection

To associate a user-defined error structure with the connection, you:
  • Use the mi_set_connection_user_data() function in the calling function to bind the structure to a connection descriptor (an MI_CONNECTION structure).
  • Use the mi_get_connection_user_data() function in the callback to obtain the structure that is bound to the connection descriptor.
Important: You can associate a user-defined error structure with a connection only if a valid connection exists and this connection does not change between the point at which the callback is registered and the point at which the exception event occurs. If you cannot guarantee that these two conditions exist, associate the user-defined error structure with the registered callback (Associate a user-defined error structure with a callback).

Client only

The following code fragment from a client LIBMI application binds the DB_ERROR_BUF user-defined structure (A sample user-defined error structure) to a connection:
int main (argc, argv)
   int argc;
   char **argv;
{
   MI_CONNECTION *conn = NULL;
   MI_CALLBACK_HANDLE *cback_hndl;
   char query[300];
   mi_integer ret;
   DB_ERROR_BUF error_buff;

   conn = mi_open(argv[1], NULL, NULL);
   if ( conn == NULL )
      /* do something appropriate */
      ...
   cback_hndl = mi_register_callback(conn, MI_Exception,
      (MI_VOID)clntexcpt_callback2, NULL, NULL);
   ret = mi_set_connection_user_data(conn,
      (MI_VOID)&error_buff);
   if ( ret == MI_ERROR )
      /* do something appropriate */
      ...
   ret = send_command(conn, query);
   if ( ret == MI_ERROR)
      {
      fprintf(stderr, "MI_Exception: level = %d",
         error_buff.error_level);
      fprintf(stderr, "SQLSTATE='%s'\n",
         error_buff.sqlstate);
      fprintf(stderr, "message = '%s'\n",
         error_buff.error_msg);
      }
   /* do something appropriate */
   ...
}
The call to mi_register_callback() does not specify the address of the user-defined structure as its fourth argument because this structure is associated with the connection. The following code implements the clntexcpt_callback() callback function, which uses the mi_get_connection_user_data() function to obtain the user-defined structure:
void clntexcpt_callback2(event_type, conn, event_info,
      user_data)
   MI_EVENT_TYPE event_type;
   MI_CONNECTION *conn; /* user-defined error buffer here */
   void *event_info;
   void *user_data;
{
   DB_ERROR_BUF *error_buf

   mi_get_connection_user_data(conn, (void **)&error_buf)
   error_buf->error_type = event_type;
   if ( event_type == MI_Exception )
      {
      error_buf->error_level = mi_error_level(event_info);
      mi_error_sql_state(event_info, error_buf->sqlstate, 6);
      mi_errmsg(event_info, error_buf->error_msg, MSG_SIZE-1);
      }
   else
      fprintf(stderr,
         "Warning! clntexcpt_callback2() fired for event "
         "%d", event_type);
   return;
}

In the preceding code fragment, the italicized portion is the only difference between this client LIBMI application and the one that registers a user-defined variable with the callback (in Associate a user-defined error structure with a callback).