Error Reporting Functions

To simplify error reporting and handling, the DB structure offers several useful methods.

In addition, you can use the db_strerror() function to directly return the error string that corresponds to a particular error number.

For example, to send all error messages for a given database handle to a callback for handling, first create your callback. Do something like this:

/* 
 * Function called to handle any database error messages
 * issued by DB. 
 */
void
my_error_handler(const DB_ENV *dbenv, const char *error_prefix,
	const char *msg)
{
  /* 
   * Put your code to handle the error prefix and error
   * message here. Note that one or both of these parameters
   * may be NULL depending on how the error message is issued
   * and how the DB handle is configured.
   */
} 

And then register the callback as follows:

#include <db.h>
#include <stdio.h>

...

DB *dbp;
int ret;

/*
 * Create a database and initialize it for error
 * reporting.
 */
ret = db_create(&dbp, NULL, 0);
if (ret != 0) {
        fprintf(stderr, "%s: %s\n", "my_program",
          db_strerror(ret));
        return(ret);
}

/* Set up error handling for this database */
dbp->set_errcall(dbp, my_error_handler);
dbp->set_errpfx(dbp, "my_example_program"); 

And to issue an error message:

ret = dbp->open(dbp, 
                NULL,
                "mydb.db", 
                NULL,
                DB_BTREE,
                DB_CREATE,
                0);
if (ret != 0) {
    dbp->err(dbp, ret,
      "Database open failed: %s", "mydb.db");
    return(ret);
}