diff --git a/src/api/bkuser_core/categories/plugins/ldap/client.py b/src/api/bkuser_core/categories/plugins/ldap/client.py index 780d45fd0..e52d3ac2c 100644 --- a/src/api/bkuser_core/categories/plugins/ldap/client.py +++ b/src/api/bkuser_core/categories/plugins/ldap/client.py @@ -12,8 +12,8 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Dict, List -import ldap3 from django.conf import settings +from django.utils.translation import gettext_lazy as _ from ldap3 import ALL, SIMPLE, Connection, Server from ldap3.utils import log as ldap3log @@ -78,16 +78,18 @@ def initialize( connection_params.update({"user": user, "password": password, "authentication": SIMPLE}) return Connection(**connection_params) - - except KeyError: + except KeyError as e: logger.exception("failed to initialize ldap server. KeyError. [url=%s]", connection_url) - raise local_exceptions.LDAPSettingNotReady - except ldap3.core.exceptions.LDAPSocketReceiveError: - logger.exception("failed to initialize ldap server. LDAPSocketReceiveError. [url=%s]", connection_url) - raise local_exceptions.LdapCannotBeInitialized - except Exception: - logger.exception("failed to initialize ldap server. [url=%s]", connection_url) - raise local_exceptions.LdapCannotBeInitialized + raise local_exceptions.LDAPSettingNotReady from e + except Exception as e: + logger.exception( + "failed to initialize ldap server. %s.%s [url=%s]", + type(e).__module__, + type(e).__name__, + connection_url, + ) + error_detail = f" ({type(e).__module__}.{type(e).__name__}: {str(e)})" + raise local_exceptions.LdapCannotBeInitialized(_("LDAP服务器连接失败") + error_detail) from e def search( self, diff --git a/src/api/bkuser_core/categories/plugins/ldap/syncer.py b/src/api/bkuser_core/categories/plugins/ldap/syncer.py index ed47252b5..bafe9f8af 100644 --- a/src/api/bkuser_core/categories/plugins/ldap/syncer.py +++ b/src/api/bkuser_core/categories/plugins/ldap/syncer.py @@ -17,6 +17,7 @@ from django.db import transaction from django.utils.encoding import force_bytes +from django.utils.translation import gettext_lazy as _ from bkuser_core.categories.exceptions import FetchDataFromRemoteFailed from bkuser_core.categories.plugins.base import DBSyncManager, Fetcher, SyncContext, Syncer, SyncStep, TypeList @@ -77,15 +78,17 @@ def _fetch_data( groups = self.client.search(start_root=basic_pull_node, force_filter_str=user_group_filter) else: groups = [] - except Exception: + except Exception as e: logger.exception("failed to get groups from remote server") - raise FetchDataFromRemoteFailed("无法获取用户组,请检查配置") + error_detail = f" ({type(e).__module__}.{type(e).__name__}: {str(e)})" + raise FetchDataFromRemoteFailed(_("无法获取用户组,请检查配置") + error_detail) try: departments = self.client.search(start_root=basic_pull_node, object_class=organization_class) - except Exception: + except Exception as e: logger.exception("failed to get departments from remote server") - raise FetchDataFromRemoteFailed("无法获取组织部门,请检查配置") + error_detail = f" ({type(e).__module__}.{type(e).__name__}: {str(e)})" + raise FetchDataFromRemoteFailed(_("无法获取组织部门,请检查配置") + error_detail) try: users = self.client.search( @@ -93,9 +96,10 @@ def _fetch_data( force_filter_str=user_filter, attributes=attributes or [], ) - except Exception: + except Exception as e: logger.exception("failed to get users from remote server") - raise FetchDataFromRemoteFailed("无法获取用户数据, 请检查配置") + error_detail = f" ({type(e).__module__}.{type(e).__name__}: {str(e)})" + raise FetchDataFromRemoteFailed(_("无法获取用户数据, 请检查配置") + error_detail) return groups, departments, users diff --git a/src/api/bkuser_core/categories/views.py b/src/api/bkuser_core/categories/views.py index 4b806ef3e..d487601f3 100644 --- a/src/api/bkuser_core/categories/views.py +++ b/src/api/bkuser_core/categories/views.py @@ -198,9 +198,9 @@ def test_connection(self, request, lookup_value): syncer_cls(instance.id, with_initialize_client=False).fetcher.client.initialize( **serializer.validated_data ) - except Exception: + except Exception as e: logger.exception("failed to test initialize category<%s>", instance.id) - raise error_codes.TEST_CONNECTION_FAILED + raise error_codes.TEST_CONNECTION_FAILED.format(str(e), replace=True) return Response() @@ -232,16 +232,15 @@ def test_fetch_data(self, request, lookup_value): try: syncer = syncer_cls(instance.id) - except Exception: + except Exception as e: logger.exception("failed to test initialize category<%s>", instance.id) - raise error_codes.TEST_CONNECTION_FAILED.f("请确保连接设置正确") + raise error_codes.TEST_CONNECTION_FAILED.f(f"请确保连接设置正确 {str(e)}") try: syncer.fetcher.test_fetch_data(serializer.validated_data) - except FetchDataFromRemoteFailed as e: - raise error_codes.TEST_FETCH_DATA_FAILED.f(f"{e}") - except Exception: # pylint: disable=broad-except - raise error_codes.TEST_FETCH_DATA_FAILED + except Exception as e: # pylint: disable=broad-except + error_detail = f" ({type(e).__module__}.{type(e).__name__}: {str(e)})" + raise error_codes.TEST_FETCH_DATA_FAILED.f(error_detail) return Response() @@ -274,17 +273,18 @@ def sync(self, request, lookup_value): request.operator, task_id, ) - raise error_codes.SYNC_DATA_FAILED.f(f"{e}") + error_detail = f" ({type(e).__module__}.{type(e).__name__}: {str(e)})" + raise error_codes.SYNC_DATA_FAILED.f(error_detail) except CoreAPIError: raise - except Exception: + except Exception as e: logger.exception( "failed to sync data. " "[instance.id=%s, operator=%s, task_id=%s]", instance.id, request.operator, task_id, ) - raise error_codes.SYNC_DATA_FAILED + raise error_codes.SYNC_DATA_FAILED.f(f"{e}") return Response({"task_id": task_id}) diff --git a/src/bkuser_global/logging.py b/src/bkuser_global/logging.py index d4d9cb43f..70ebdc16f 100644 --- a/src/bkuser_global/logging.py +++ b/src/bkuser_global/logging.py @@ -43,8 +43,8 @@ def get_logging(logging_type: int, **kwargs) -> dict: "%(funcName)s %(process)d %(thread)d %(request_id)s %(message)s" ), }, - "simple": {"format": "%(levelname)s %(message)s"}, - "iam": {"format": "[IAM] %(asctime)s %(message)s"}, + "simple": {"format": "%(levelname)s [%(asctime)s] %(message)s"}, + "iam": {"format": "[IAM] %(levelname)s [%(asctime)s] %(message)s"}, }