Skip to content

Commit e37552d

Browse files
authored
CDRIVER-4028 Print correct error message when DNS resolution fails (#811)
* Print correct error message when DNS resolution fails * Drop duplicate case value
1 parent 36189eb commit e37552d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/libmongoc/src/mongoc/mongoc-client.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ typedef bool (*mongoc_rr_callback_t) (const char *service,
299299
mongoc_rr_data_t *rr_data,
300300
bson_error_t *error);
301301

302+
static const char *
303+
_mongoc_hstrerror (int code)
304+
{
305+
switch (code) {
306+
case HOST_NOT_FOUND:
307+
return "The specified host is unknown.";
308+
case NO_ADDRESS:
309+
return "The requested name is valid but does not have an IP address.";
310+
case NO_RECOVERY:
311+
return "A nonrecoverable name server error occurred.";
312+
case TRY_AGAIN:
313+
return "A temporary error occurred on an authoritative name server. Try "
314+
"again later.";
315+
default:
316+
return "An unknown error occurred.";
317+
}
318+
}
319+
302320
static bool
303321
srv_callback (const char *service,
304322
ns_msg *ns_answer,
@@ -328,7 +346,7 @@ srv_callback (const char *service,
328346
if (size < 1) {
329347
DNS_ERROR ("Invalid record in SRV answer for \"%s\": \"%s\"",
330348
service,
331-
strerror (h_errno));
349+
_mongoc_hstrerror (h_errno));
332350
}
333351

334352
if (!_mongoc_host_list_from_hostport_with_err (
@@ -474,7 +492,7 @@ _mongoc_get_rr_search (const char *service,
474492
DNS_ERROR ("Failed to look up %s record \"%s\": %s",
475493
rr_type_name,
476494
service,
477-
strerror (h_errno));
495+
_mongoc_hstrerror (h_errno));
478496
}
479497
} while (size >= buffer_size);
480498

@@ -495,7 +513,7 @@ _mongoc_get_rr_search (const char *service,
495513
i,
496514
rr_type_name,
497515
service,
498-
strerror (h_errno));
516+
_mongoc_hstrerror (h_errno));
499517
}
500518

501519
/* Skip records that don't match the ones we requested. CDRIVER-3628 shows

0 commit comments

Comments
 (0)