perf: defer LDAP import

This commit is contained in:
Ankush Menat 2023-06-24 12:28:20 +05:30 committed by Ankush Menat
parent 8a83226c60
commit 45f8aff909
2 changed files with 26 additions and 4 deletions

View file

@ -1,7 +1,10 @@
# Copyright (c) 2015, Frappe Technologies and Contributors
# License: MIT. See LICENSE
from ldap3.core.exceptions import LDAPException, LDAPInappropriateAuthenticationResult
import frappe
from frappe.tests.utils import FrappeTestCase
from frappe.utils.error import _is_ldap_exception
# test_records = frappe.get_test_records('Error Log')
@ -12,3 +15,9 @@ class TestErrorLog(FrappeTestCase):
doc = frappe.new_doc("Error Log")
error = doc.log_error("This is an error")
self.assertEqual(error.doctype, "Error Log")
def test_ldap_exceptions(self):
exc = [LDAPException, LDAPInappropriateAuthenticationResult]
for e in exc:
self.assertTrue(_is_ldap_exception(e()))

View file

@ -11,8 +11,6 @@ import pydoc
import sys
import traceback
from ldap3.core.exceptions import LDAPException
import frappe
from frappe.utils import cstr, encode
@ -20,16 +18,31 @@ EXCLUDE_EXCEPTIONS = (
frappe.AuthenticationError,
frappe.CSRFTokenError, # CSRF covers OAuth too
frappe.SecurityException,
LDAPException,
frappe.InReadOnlyMode,
)
LDAP_BASE_EXCEPTION = "LDAPException"
def _is_ldap_exception(e):
"""Check if exception is from LDAP library.
This is a hack but ensures that LDAP is not imported unless it's required. This is tested in
unittests in case the exception changes in future.
"""
for t in type(e).__mro__:
if t.__name__ == LDAP_BASE_EXCEPTION:
return True
return False
def make_error_snapshot(exception):
if frappe.conf.disable_error_snapshot:
return
if isinstance(exception, EXCLUDE_EXCEPTIONS):
if isinstance(exception, EXCLUDE_EXCEPTIONS) or _is_ldap_exception(exception):
return
logger = frappe.logger(with_more_info=True)