Skip to content

Commit e1be09a

Browse files
committed
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 7f297c6 commit e1be09a

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 (
@@ -473,7 +491,7 @@ _mongoc_get_rr_search (const char *service,
473491
DNS_ERROR ("Failed to look up %s record \"%s\": %s",
474492
rr_type_name,
475493
service,
476-
strerror (h_errno));
494+
_mongoc_hstrerror (h_errno));
477495
}
478496
} while (size >= buffer_size);
479497

@@ -494,7 +512,7 @@ _mongoc_get_rr_search (const char *service,
494512
i,
495513
rr_type_name,
496514
service,
497-
strerror (h_errno));
515+
_mongoc_hstrerror (h_errno));
498516
}
499517

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

0 commit comments

Comments
 (0)