Skip to content

Commit e688cb4

Browse files
committed
CDRIVER-2581 improve opmsg cmd error msgs
1 parent 2dc318f commit e688cb4

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

src/mongoc/mongoc-cluster.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ _bson_error_message_printf (bson_error_t *error, const char *format, ...)
146146
}
147147
}
148148

149-
#define RUN_CMD_ERR(_domain, _code, _msg) \
149+
#define RUN_CMD_ERR_DECORATE \
150150
do { \
151-
bson_set_error (error, _domain, _code, _msg); \
152151
_bson_error_message_printf ( \
153152
error, \
154153
"Failed to send \"%s\" command with database \"%s\": %s", \
@@ -157,6 +156,11 @@ _bson_error_message_printf (bson_error_t *error, const char *format, ...)
157156
error->message); \
158157
} while (0)
159158

159+
#define RUN_CMD_ERR(_domain, _code, ...) \
160+
do { \
161+
bson_set_error (error, _domain, _code, __VA_ARGS__); \
162+
RUN_CMD_ERR_DECORATE; \
163+
} while (0)
160164

161165
/*
162166
*--------------------------------------------------------------------------
@@ -259,13 +263,7 @@ mongoc_cluster_run_command_opquery (mongoc_cluster_t *cluster,
259263
mongoc_cluster_disconnect_node (cluster, server_id, true, error);
260264

261265
/* add info about the command to writev_full's error message */
262-
_bson_error_message_printf (
263-
error,
264-
"Failed to send \"%s\" command with database \"%s\": %s",
265-
cmd->command_name,
266-
cmd->db_name,
267-
error->message);
268-
266+
RUN_CMD_ERR_DECORATE;
269267
GOTO (done);
270268
}
271269

@@ -2579,6 +2577,8 @@ mongoc_cluster_run_opmsg (mongoc_cluster_t *cluster,
25792577
cluster->sockettimeoutms,
25802578
error);
25812579
if (!ok) {
2580+
/* add info about the command to writev_full's error message */
2581+
RUN_CMD_ERR_DECORATE;
25822582
mongoc_cluster_disconnect_node (
25832583
cluster, server_stream->sd->id, true, error);
25842584
bson_free (output);
@@ -2590,6 +2590,7 @@ mongoc_cluster_run_opmsg (mongoc_cluster_t *cluster,
25902590
ok = _mongoc_buffer_append_from_stream (
25912591
&buffer, server_stream->stream, 4, cluster->sockettimeoutms, error);
25922592
if (!ok) {
2593+
RUN_CMD_ERR_DECORATE;
25932594
mongoc_cluster_disconnect_node (
25942595
cluster, server_stream->sd->id, true, error);
25952596
bson_free (output);
@@ -2602,8 +2603,7 @@ mongoc_cluster_run_opmsg (mongoc_cluster_t *cluster,
26022603
memcpy (&msg_len, buffer.data, 4);
26032604
msg_len = BSON_UINT32_FROM_LE (msg_len);
26042605
if ((msg_len < 16) || (msg_len > server_stream->sd->max_msg_size)) {
2605-
bson_set_error (
2606-
error,
2606+
RUN_CMD_ERR (
26072607
MONGOC_ERROR_PROTOCOL,
26082608
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
26092609
"Message size %d is not within expected range 16-%d bytes",
@@ -2623,6 +2623,7 @@ mongoc_cluster_run_opmsg (mongoc_cluster_t *cluster,
26232623
cluster->sockettimeoutms,
26242624
error);
26252625
if (!ok) {
2626+
RUN_CMD_ERR_DECORATE;
26262627
mongoc_cluster_disconnect_node (
26272628
cluster, server_stream->sd->id, true, error);
26282629
bson_free (output);
@@ -2633,8 +2634,7 @@ mongoc_cluster_run_opmsg (mongoc_cluster_t *cluster,
26332634

26342635
ok = _mongoc_rpc_scatter (&rpc, buffer.data, buffer.len);
26352636
if (!ok) {
2636-
bson_set_error (error,
2637-
MONGOC_ERROR_PROTOCOL,
2637+
RUN_CMD_ERR (MONGOC_ERROR_PROTOCOL,
26382638
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
26392639
"Malformed message from server");
26402640
bson_free (output);
@@ -2648,8 +2648,7 @@ mongoc_cluster_run_opmsg (mongoc_cluster_t *cluster,
26482648

26492649
output = bson_realloc (output, len);
26502650
if (!_mongoc_rpc_decompress (&rpc, (uint8_t *) output, len)) {
2651-
bson_set_error (error,
2652-
MONGOC_ERROR_PROTOCOL,
2651+
RUN_CMD_ERR (MONGOC_ERROR_PROTOCOL,
26532652
MONGOC_ERROR_PROTOCOL_INVALID_REPLY,
26542653
"Could not decompress message from server");
26552654
mongoc_cluster_disconnect_node (

tests/test-mongoc-cluster.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,69 @@ test_cluster_ismaster_hangup (void)
13781378
_test_cluster_ismaster_fails (true);
13791379
}
13801380

1381+
static void
1382+
_test_cluster_command_error (bool use_op_msg)
1383+
{
1384+
mock_server_t *server;
1385+
mongoc_client_t *client;
1386+
bson_error_t err;
1387+
request_t *request;
1388+
future_t *future;
1389+
1390+
if (use_op_msg) {
1391+
server = mock_server_with_autoismaster (WIRE_VERSION_OP_MSG);
1392+
} else {
1393+
server = mock_server_with_autoismaster (WIRE_VERSION_OP_MSG - 1);
1394+
}
1395+
mock_server_run (server);
1396+
client = mongoc_client_new_from_uri (mock_server_get_uri (server));
1397+
future = future_client_command_simple (client,
1398+
"db",
1399+
tmp_bson ("{'ping': 1}"),
1400+
NULL /* opts */,
1401+
NULL /* read prefs */,
1402+
&err);
1403+
if (use_op_msg) {
1404+
request = mock_server_receives_msg (
1405+
server, MONGOC_QUERY_NONE, tmp_bson ("{'ping': 1}"));
1406+
} else {
1407+
request = mock_server_receives_command (
1408+
server, "db", MONGOC_QUERY_SLAVE_OK, "{'ping': 1}");
1409+
}
1410+
mock_server_hangs_up (request);
1411+
BSON_ASSERT (!future_get_bool (future));
1412+
future_destroy (future);
1413+
request_destroy (request);
1414+
if (use_op_msg) {
1415+
/* _mongoc_buffer_append_from_stream, used by opmsg gives more detail. */
1416+
ASSERT_ERROR_CONTAINS (err,
1417+
MONGOC_ERROR_STREAM,
1418+
MONGOC_ERROR_STREAM_SOCKET,
1419+
"Failed to send \"ping\" command with database "
1420+
"\"db\": Failed to read 4 bytes: socket error or "
1421+
"timeout");
1422+
} else {
1423+
ASSERT_ERROR_CONTAINS (err,
1424+
MONGOC_ERROR_STREAM,
1425+
MONGOC_ERROR_STREAM_SOCKET,
1426+
"Failed to send \"ping\" command with database "
1427+
"\"db\": socket error or timeout");
1428+
}
1429+
mock_server_destroy (server);
1430+
mongoc_client_destroy (client);
1431+
}
1432+
1433+
static void
1434+
test_cluster_command_error_op_msg ()
1435+
{
1436+
_test_cluster_command_error (true);
1437+
}
1438+
1439+
static void
1440+
test_cluster_command_error_op_query ()
1441+
{
1442+
_test_cluster_command_error (false);
1443+
}
13811444

13821445
void
13831446
test_cluster_install (TestSuite *suite)
@@ -1503,4 +1566,10 @@ test_cluster_install (TestSuite *suite)
15031566
suite, "/Cluster/ismaster_fails", test_cluster_ismaster_fails);
15041567
TestSuite_AddMockServerTest (
15051568
suite, "/Cluster/ismaster_hangup", test_cluster_ismaster_hangup);
1569+
TestSuite_AddMockServerTest (suite,
1570+
"/Cluster/command_error/op_msg",
1571+
test_cluster_command_error_op_msg);
1572+
TestSuite_AddMockServerTest (suite,
1573+
"/Cluster/command_error/op_query",
1574+
test_cluster_command_error_op_query);
15061575
}

0 commit comments

Comments
 (0)