diff --git a/examples/c_api/errors.c b/examples/c_api/errors.c index 5116a7281ca..fcd9f83363c 100644 --- a/examples/c_api/errors.c +++ b/examples/c_api/errors.c @@ -31,22 +31,30 @@ */ #include -#include +#include -void print_error(tiledb_ctx_t* ctx); +void print_last_error(tiledb_ctx_t* ctx); +void print_error(tiledb_error_t* err); int main() { // Create TileDB context tiledb_ctx_t* ctx; - tiledb_ctx_alloc(NULL, &ctx); + tiledb_error_t* err; + int rc = tiledb_ctx_alloc_with_error(NULL, &ctx, &err); + if (rc == TILEDB_OK) + printf("Context created successfully!\n"); + else if (rc == TILEDB_ERR) { + print_error(err); + return 1; + } // Create a group. The code below creates a group `my_group` and prints a // message because (normally) it will succeed. - int rc = tiledb_group_create(ctx, "my_group"); + tiledb_group_create(ctx, "my_group"); if (rc == TILEDB_OK) printf("Group created successfully!\n"); else if (rc == TILEDB_ERR) - print_error(ctx); + print_last_error(ctx); // Create the same group again. f we attempt to create the same group // `my_group` as shown below, TileDB will return an error and the example @@ -55,7 +63,7 @@ int main() { if (rc == TILEDB_OK) printf("Group created successfully!\n"); else if (rc == TILEDB_ERR) - print_error(ctx); + print_last_error(ctx); // Clean up tiledb_ctx_free(&ctx); @@ -63,16 +71,20 @@ int main() { return 0; } -void print_error(tiledb_ctx_t* ctx) { +void print_last_error(tiledb_ctx_t* ctx) { // Retrieve the last error that occurred tiledb_error_t* err = NULL; tiledb_ctx_get_last_error(ctx, &err); + print_error(err); + + // Clean up + tiledb_error_free(&err); +} + +void print_error(tiledb_error_t* err) { // Retrieve the error message by invoking `tiledb_error_message`. const char* msg; tiledb_error_message(err, &msg); printf("%s\n", msg); - - // Clean up - tiledb_error_free(&err); } diff --git a/tiledb/api/c_api/context/context_api.cc b/tiledb/api/c_api/context/context_api.cc index f9a47f53408..7b58fc40692 100644 --- a/tiledb/api/c_api/context/context_api.cc +++ b/tiledb/api/c_api/context/context_api.cc @@ -61,6 +61,17 @@ capi_return_t tiledb_ctx_alloc( return TILEDB_OK; } +/** + * Transparent forwarder to tiledb_ctx_alloc. + * + * Must be a separate function with its own name due to requirements of the + * CAPI_INTERFACE macro. + */ +capi_return_t tiledb_ctx_alloc_with_error( + tiledb_config_handle_t* config, tiledb_ctx_handle_t** ctx) { + return tiledb::api::tiledb_ctx_alloc(config, ctx); +} + void tiledb_ctx_free(tiledb_ctx_t** ctx) { ensure_output_pointer_is_valid(ctx); ensure_context_is_valid(*ctx); @@ -150,11 +161,7 @@ CAPI_INTERFACE( tiledb_config_handle_t* config, tiledb_ctx_handle_t** ctx, tiledb_error_handle_t** error) { - /* - * Wrapped with the `api_entry_error` variation. Note that the same function - * is wrapped with `api_entry_plain` above. - */ - return tiledb::api::api_entry_error( + return tiledb::api::api_entry_error( error, config, ctx); }