1MONGOC_CURSOR_ERROR_DOCUMENT(3) libmongoc MONGOC_CURSOR_ERROR_DOCUMENT(3)
2
3
4
6 bool
7 mongoc_cursor_error_document (mongoc_cursor_t *cursor,
8 bson_error_t *error,
9 const bson_t **reply);
10
12 • cursor: A mongoc_cursor_t.
13
14 • error: An optional location for a bson_error_t or NULL.
15
16 • reply: A location for a bson_t.
17
19 This function checks to see if an error has occurred while iterating
20 the cursor.
21
22 If an error occurred client-side, for example if there was a network
23 error or timeout, or the cursor was created with invalid parameters,
24 then reply is set to an empty BSON document. If an error occurred
25 server-side, reply is set to the server's reply document with informa‐
26 tion about the error.
27
29 Errors are propagated via the error and reply parameters.
30
32 False if no error has occurred, otherwise true and error is set.
33
34 If the function returns true and reply is not NULL, then reply is set
35 to a pointer to a BSON document, which is either empty or the server's
36 error response. The document is invalid after the cursor is freed with
37 mongoc_cursor_destroy().
38
40 This example shows the difference between a client-side and server-side
41 error. If the client cannot connect to the server, for example, the er‐
42 ror message includes "No suitable servers found" and reply is set to an
43 empty BSON document.
44
45 On the other hand, if the client connects to the server successfully
46 and attempts to execute an invalid query, the error message comes from
47 the server and reply is set to the server's reply document, with fields
48 errmsg and code.
49
50 void
51 run_query (const char *uri_str, const bson_t *query)
52 {
53 mongoc_client_t *client;
54 mongoc_collection_t *collection;
55 mongoc_cursor_t *cursor;
56 const bson_t *doc;
57 bson_error_t error;
58 const bson_t *reply;
59 char *str;
60
61 client = mongoc_client_new (uri_str);
62
63 mongoc_client_set_error_api (client, 2);
64
65 collection = mongoc_client_get_collection (client, "db", "collection");
66 cursor = mongoc_collection_find_with_opts (
67 collection,
68 query,
69 NULL, /* additional options */
70 NULL); /* read prefs, NULL for default */
71
72 /* this loop is never run: mongoc_cursor_next immediately returns false */
73 while (mongoc_cursor_next (cursor, &doc)) {
74 }
75
76 if (mongoc_cursor_error_document (cursor, &error, &reply)) {
77 str = bson_as_json (reply, NULL);
78 fprintf (stderr, "Cursor Failure: %s\nReply: %s\n", error.message, str);
79 bson_free (str);
80 }
81
82 mongoc_cursor_destroy (cursor);
83 mongoc_collection_destroy (collection);
84 mongoc_client_destroy (client);
85 }
86
87 int
88 main (int argc, char *argv[])
89 {
90 bson_t *good_query;
91 bson_t *bad_query;
92
93 mongoc_init ();
94
95 /* find documents matching the query {"x": 1} */
96 good_query = BCON_NEW ("x", BCON_INT64 (1));
97
98 /* Cause a network error. This will print an error and empty reply document:
99 *
100 * Cursor Failure: No suitable servers found (`serverSelectionTryOnce` set):
101 * [Failed to resolve 'fake-domain']
102 *
103 * Reply: { }
104 *
105 */
106 run_query ("mongodb://fake-domain/?appname=cursor-example", good_query);
107
108 /* invalid: {"x": {"$badOperator": 1}} */
109 bad_query = BCON_NEW ("x", "{", "$badOperator", BCON_INT64 (1), "}");
110
111 /* Cause a server error. This will print an error and server reply document:
112 *
113 * Cursor Failure: unknown operator: $badOperator
114 *
115 * Reply:
116 * {"ok": 0.0,
117 * "errmsg":"unknown operator: $badOperator",
118 * "code": 2,
119 * "codeName":"BadValue"
120 * }
121 *
122 */
123 run_query ("mongodb://localhost/?appname=cursor-example", bad_query);
124
125 bson_destroy (good_query);
126 bson_destroy (bad_query);
127
128 mongoc_cleanup ();
129
130 return 0;
131 }
132
134 MongoDB, Inc
135
137 2017-present, MongoDB, Inc
138
139
140
141
1421.25.1 Nov 08, 2023 MONGOC_CURSOR_ERROR_DOCUMENT(3)