Don't crash on LDAP errors

This commit is contained in:
Martin Pépin 2020-06-28 23:48:20 +02:00
parent 56f1edebe3
commit de1bba3695
No known key found for this signature in database
GPG key ID: E7520278B1774448

View file

@ -1,3 +1,4 @@
import logging
from collections import namedtuple
from dal import autocomplete
@ -12,6 +13,9 @@ else:
ldap = None
django_logger = logging.getLogger("django.request")
class SearchUnit:
"""Base class for all the search utilities.
@ -128,6 +132,7 @@ class LDAPSearch(SearchUnit):
if ldap is None or query == "(&)":
return []
try:
ldap_obj = ldap.initialize(self.ldap_server_url)
res = ldap_obj.search_s(
self.domain_component, ldap.SCOPE_SUBTREE, query, self.search_fields
@ -139,6 +144,9 @@ class LDAPSearch(SearchUnit):
)
for (_, attrs) in res
]
except ldap.LDAPError as err:
django_logger.error("An LDAP error occurred", exc_info=err)
return []
# ---