1212#define PARTITION_KEY_VALUE "test-partition"
1313#define PARTITION_KEY_PATH "/partitionKey"
1414
15+ void display_error (const cosmos_error * error ) {
16+ printf ("Error Code: %d\n" , error -> code );
17+ if (error -> message ) {
18+ printf ("Error Message: %s\n" , error -> message );
19+ }
20+ if (error -> detail ) {
21+ printf ("Error Details: %s\n" , error -> detail );
22+ cosmos_string_free (error -> detail );
23+ }
24+ }
25+
1526int main () {
1627 cosmos_enable_tracing ();
1728
@@ -35,38 +46,46 @@ int main() {
3546 printf ("Database: %s\n" , database_name );
3647 printf ("Container: test-container\n" );
3748
38- cosmos_error error = {0 };
49+ cosmos_runtime_context * runtime = cosmos_runtime_context_create (NULL );
50+ if (!runtime ) {
51+ printf ("Failed to create runtime context\n" );
52+ return 1 ;
53+ }
54+ cosmos_call_context ctx ;
55+ ctx .runtime_context = runtime ;
56+ ctx .include_error_details = true;
57+
3958 cosmos_client * client = NULL ;
4059 cosmos_database_client * database = NULL ;
4160 cosmos_container_client * container = NULL ;
42- char * read_json = NULL ;
61+ const char * read_json = NULL ;
4362 int result = 0 ;
4463 int database_created = 0 ;
4564 int container_created = 0 ;
4665
4766 // Create Cosmos client
48- cosmos_error_code code = cosmos_client_create_with_key (endpoint , key , & client , & error );
67+ cosmos_error_code code = cosmos_client_create_with_key (& ctx , endpoint , key , & client );
4968 if (code != COSMOS_ERROR_CODE_SUCCESS ) {
50- printf ( "Failed to create Cosmos client: %s (code: %d)\n" , error . message , error . code );
69+ display_error ( & ctx . error );
5170 result = 1 ;
5271 goto cleanup ;
5372 }
5473 printf ("✓ Created Cosmos client\n" );
5574
5675 // Create database
57- code = cosmos_client_create_database (client , database_name , & database , & error );
76+ code = cosmos_client_create_database (& ctx , client , database_name , & database );
5877 if (code != COSMOS_ERROR_CODE_SUCCESS ) {
59- printf ( "Failed to create database: %s (code: %d)\n" , error . message , error . code );
78+ display_error ( & ctx . error );
6079 result = 1 ;
6180 goto cleanup ;
6281 }
6382 database_created = 1 ;
6483 printf ("✓ Created database: %s\n" , database_name );
6584
6685 // Create container with partition key
67- code = cosmos_database_create_container (database , "test-container" , PARTITION_KEY_PATH , & container , & error );
86+ code = cosmos_database_create_container (& ctx , database , "test-container" , PARTITION_KEY_PATH , & container );
6887 if (code != COSMOS_ERROR_CODE_SUCCESS ) {
69- printf ( "Failed to create container: %s (code: %d)\n" , error . message , error . code );
88+ display_error ( & ctx . error );
7089 result = 1 ;
7190 goto cleanup ;
7291 }
@@ -82,18 +101,18 @@ int main() {
82101 printf ("Upserting document: %s\n" , json_data );
83102
84103 // Upsert the item
85- code = cosmos_container_upsert_item (container , PARTITION_KEY_VALUE , json_data , & error );
104+ code = cosmos_container_upsert_item (& ctx , container , PARTITION_KEY_VALUE , json_data );
86105 if (code != COSMOS_ERROR_CODE_SUCCESS ) {
87- printf ( "Failed to upsert item: %s (code: %d)\n" , error . message , error . code );
106+ display_error ( & ctx . error );
88107 result = 1 ;
89108 goto cleanup ;
90109 }
91110 printf ("✓ Upserted item successfully\n" );
92111
93112 // Read the item back
94- code = cosmos_container_read_item (container , PARTITION_KEY_VALUE , ITEM_ID , & read_json , & error );
113+ code = cosmos_container_read_item (& ctx , container , PARTITION_KEY_VALUE , ITEM_ID , & read_json );
95114 if (code != COSMOS_ERROR_CODE_SUCCESS ) {
96- printf ( "Failed to read item: %s (code: %d)\n" , error . message , error . code );
115+ display_error ( & ctx . error );
97116 result = 1 ;
98117 goto cleanup ;
99118 }
@@ -127,10 +146,9 @@ int main() {
127146 // Delete database (this will also delete the container)
128147 if (database && database_created ) {
129148 printf ("Deleting database: %s\n" , database_name );
130- cosmos_error delete_error = {0 };
131- cosmos_error_code delete_code = cosmos_database_delete (database , & delete_error );
149+ cosmos_error_code delete_code = cosmos_database_delete (& ctx , database );
132150 if (delete_code != COSMOS_ERROR_CODE_SUCCESS ) {
133- printf ( "Failed to delete database: %s (code: %d)\n" , delete_error . message , delete_error . code );
151+ display_error ( & ctx . error );
134152 } else {
135153 printf ("✓ Deleted database successfully\n" );
136154 }
@@ -145,9 +163,6 @@ int main() {
145163 if (client ) {
146164 cosmos_client_free (client );
147165 }
148- if (error .message ) {
149- cosmos_error_free (& error );
150- }
151166
152167 return result ;
153168}
0 commit comments