Error Handling

Conduit’s APIs emit three types of messages for logging and error handling:

Message Type Description
Info General Information
Warning Recoverable Error
Error Fatal Error

Default Error Handlers

Conduit provides a default handler for each message type:

Message Type Default Action
Info Prints the message to standard out
Warning Throws a C++ Exception (conduit::Error instance)
Error Throws a C++ Exception (conduit::Error instance)

Using Custom Error Handlers

The conduit::utils namespace provides functions to override each of the three default handlers with a method that provides the following signature:

void my_handler(const std::string &msg,
                const std::string &file,
                int line)
  // your handling code here ...


Here is an example that re-wires all three error handlers to print to standard out:

void my_info_handler(const std::string &msg,
                     const std::string &file,
                     int line)
    std::cout << "[INFO] " << msg << std::endl;

void my_warning_handler(const std::string &msg,
                        const std::string &file,
                        int line)
    std::cout << "[WARNING!] " << msg << std::endl;

void my_error_handler(const std::string &msg,
                      const std::string &file,
                      int line)
    std::cout << "[ERROR!] " << msg << std::endl;
    // errors are considered fatal, aborting or unwinding the 
    // call stack with an exception are the only viable options
    throw conduit::Error(msg,file,line);
// rewire error handlers

// emit an example info message
CONDUIT_INFO("An info message");

Node n;

// emit an example warning message

// using "as" for wrong type emits a warning, returns a default value (0.0)
float32 v = n["my_value"].as_float32();

// emit an example error message

    // fetching a non-existant path from a const Node emits an error
    const Node &n_my_value = n["my_value"];
catch(conduit::Error e)
    // pass
[INFO] An info message
[WARNING!] Node::as_float32() const -- DataType float64 at path my_value does not equal expected DataType float32
[ERROR!] Cannot fetch_existing, Node(my_value) is not an object

Using Restoring Default Handlers

The default handlers are part of the conduit::utils interface, so you can restore them using:

// restore default handlers