Skip to content

Commit 196b489

Browse files
teo-tsirpanisihnorton
authored andcommitted
Update an example to use tiledb_ctx_alloc_with_error.
This will validate that the symbol exists.
1 parent dd3e174 commit 196b489

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

examples/c_api/errors.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,30 @@
3131
*/
3232

3333
#include <stdio.h>
34-
#include <tiledb/tiledb.h>
34+
#include <tiledb/tiledb_experimental.h>
3535

36-
void print_error(tiledb_ctx_t* ctx);
36+
void print_last_error(tiledb_ctx_t* ctx);
37+
void print_error(tiledb_error_t* err);
3738

3839
int main() {
3940
// Create TileDB context
4041
tiledb_ctx_t* ctx;
41-
tiledb_ctx_alloc(NULL, &ctx);
42+
tiledb_error_t* err;
43+
int rc = tiledb_ctx_alloc_with_error(NULL, &ctx, &err);
44+
if (rc == TILEDB_OK)
45+
printf("Context created successfully!\n");
46+
else if (rc == TILEDB_ERR) {
47+
print_error(err);
48+
return 1;
49+
}
4250

4351
// Create a group. The code below creates a group `my_group` and prints a
4452
// message because (normally) it will succeed.
45-
int rc = tiledb_group_create(ctx, "my_group");
53+
tiledb_group_create(ctx, "my_group");
4654
if (rc == TILEDB_OK)
4755
printf("Group created successfully!\n");
4856
else if (rc == TILEDB_ERR)
49-
print_error(ctx);
57+
print_last_error(ctx);
5058

5159
// Create the same group again. f we attempt to create the same group
5260
// `my_group` as shown below, TileDB will return an error and the example
@@ -55,24 +63,28 @@ int main() {
5563
if (rc == TILEDB_OK)
5664
printf("Group created successfully!\n");
5765
else if (rc == TILEDB_ERR)
58-
print_error(ctx);
66+
print_last_error(ctx);
5967

6068
// Clean up
6169
tiledb_ctx_free(&ctx);
6270

6371
return 0;
6472
}
6573

66-
void print_error(tiledb_ctx_t* ctx) {
74+
void print_last_error(tiledb_ctx_t* ctx) {
6775
// Retrieve the last error that occurred
6876
tiledb_error_t* err = NULL;
6977
tiledb_ctx_get_last_error(ctx, &err);
7078

79+
print_error(err);
80+
81+
// Clean up
82+
tiledb_error_free(&err);
83+
}
84+
85+
void print_error(tiledb_error_t* err) {
7186
// Retrieve the error message by invoking `tiledb_error_message`.
7287
const char* msg;
7388
tiledb_error_message(err, &msg);
7489
printf("%s\n", msg);
75-
76-
// Clean up
77-
tiledb_error_free(&err);
7890
}

tiledb/api/c_api/context/context_api.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ capi_return_t tiledb_ctx_alloc(
6161
return TILEDB_OK;
6262
}
6363

64+
/**
65+
* Transparent forwarder to tiledb_ctx_alloc.
66+
*
67+
* Must be a separate function with its own name due to requirements of the
68+
* CAPI_INTERFACE macro.
69+
*/
70+
capi_return_t tiledb_ctx_alloc_with_error(
71+
tiledb_config_handle_t* config, tiledb_ctx_handle_t** ctx) {
72+
return tiledb::api::tiledb_ctx_alloc(config, ctx);
73+
}
74+
6475
void tiledb_ctx_free(tiledb_ctx_t** ctx) {
6576
ensure_output_pointer_is_valid(ctx);
6677
ensure_context_is_valid(*ctx);
@@ -150,11 +161,7 @@ CAPI_INTERFACE(
150161
tiledb_config_handle_t* config,
151162
tiledb_ctx_handle_t** ctx,
152163
tiledb_error_handle_t** error) {
153-
/*
154-
* Wrapped with the `api_entry_error` variation. Note that the same function
155-
* is wrapped with `api_entry_plain` above.
156-
*/
157-
return tiledb::api::api_entry_error<tiledb::api::tiledb_ctx_alloc>(
164+
return tiledb::api::api_entry_error<tiledb::api::tiledb_ctx_alloc_with_error>(
158165
error, config, ctx);
159166
}
160167

0 commit comments

Comments
 (0)