Skip to content

Commit

Permalink
Update an example to use tiledb_ctx_alloc_with_error.
Browse files Browse the repository at this point in the history
This will validate that the symbol exists.
  • Loading branch information
teo-tsirpanis authored and ihnorton committed Oct 25, 2024
1 parent dd3e174 commit 196b489
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
32 changes: 22 additions & 10 deletions examples/c_api/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,30 @@
*/

#include <stdio.h>
#include <tiledb/tiledb.h>
#include <tiledb/tiledb_experimental.h>

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
Expand All @@ -55,24 +63,28 @@ 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);

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);
}
17 changes: 12 additions & 5 deletions tiledb/api/c_api/context/context_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<tiledb::api::tiledb_ctx_alloc>(
return tiledb::api::api_entry_error<tiledb::api::tiledb_ctx_alloc_with_error>(
error, config, ctx);
}

Expand Down

0 comments on commit 196b489

Please sign in to comment.