Handling Errors

Many functions in Libbson can result in errors. To simplify this, Libbson contains a structure called bson_error_t. This structure enapsulates an error from the underlying system and can be passed back up the stack. Think of it as an exception as an out parameter.

The structure contains domain and code fields that can be used to check for specific error conditions. The value of these fields is API specific. If you simply want to print an error message, use the message field of bson_error_t.

Functions that can result in error specify the return value that indicates the error out parameter was initialized. Typically this is 0 or NULL, but may sometimes be -1 depending on the API.

This structure should be stack-allocated and requires no explicit freeing.