Skip to content

Commit a5eba6e

Browse files
author
Christian Hergert
committed
cursor: unwrap errmsg from failed command execution.
1 parent f437f6d commit a5eba6e

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

mongoc/mongoc-cursor.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,24 @@ _mongoc_cursor_populate_error (mongoc_cursor_t *cursor,
198198
bson_iter_t iter;
199199
const char *msg = "Unknown query failure";
200200

201-
BSON_ASSERT(cursor);
202-
BSON_ASSERT(doc);
203-
BSON_ASSERT(error);
201+
BSON_ASSERT (cursor);
202+
BSON_ASSERT (doc);
203+
BSON_ASSERT (error);
204+
205+
if (bson_iter_init_find (&iter, doc, "code") &&
206+
BSON_ITER_HOLDS_INT32 (&iter)) {
207+
code = bson_iter_int32 (&iter);
208+
}
204209

205-
if (bson_iter_init_find(&iter, doc, "code") &&
206-
BSON_ITER_HOLDS_INT32(&iter)) {
207-
code = bson_iter_int32(&iter);
210+
if (bson_iter_init_find (&iter, doc, "$err") &&
211+
BSON_ITER_HOLDS_UTF8 (&iter)) {
212+
msg = bson_iter_utf8 (&iter, NULL);
208213
}
209214

210-
if (bson_iter_init_find(&iter, doc, "$err") &&
211-
BSON_ITER_HOLDS_UTF8(&iter)) {
212-
msg = bson_iter_utf8(&iter, NULL);
215+
if (cursor->is_command &&
216+
bson_iter_init_find (&iter, doc, "errmsg") &&
217+
BSON_ITER_HOLDS_UTF8 (&iter)) {
218+
msg = bson_iter_utf8 (&iter, NULL);
213219
}
214220

215221
bson_set_error(error, MONGOC_ERROR_QUERY, code, "%s", msg);

0 commit comments

Comments
 (0)