mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 20:48:01 +08:00
Merge branch 'fix/enterprise-api-error-handling' into release/e-1.12.1
# Conflicts: # api/app_factory.py # api/services/enterprise/enterprise_service.py
This commit is contained in:
commit
7a1f0e3258
@ -11,6 +11,7 @@ from controllers.console.error import UnauthorizedAndForceLogout
|
|||||||
from core.logging.context import init_request_context
|
from core.logging.context import init_request_context
|
||||||
from dify_app import DifyApp
|
from dify_app import DifyApp
|
||||||
from services.enterprise.enterprise_service import EnterpriseService
|
from services.enterprise.enterprise_service import EnterpriseService
|
||||||
|
from services.feature_service import LicenseStatus
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -38,8 +39,8 @@ def create_flask_app_with_configs() -> DifyApp:
|
|||||||
# When license expires, block all API access except bootstrap endpoints needed
|
# When license expires, block all API access except bootstrap endpoints needed
|
||||||
# for the frontend to load the license expiration page without infinite reloads.
|
# for the frontend to load the license expiration page without infinite reloads.
|
||||||
if dify_config.ENTERPRISE_ENABLED:
|
if dify_config.ENTERPRISE_ENABLED:
|
||||||
is_console_api = request.path.startswith("/console/api")
|
is_console_api = request.path.startswith("/console/api/")
|
||||||
is_webapp_api = request.path.startswith("/api") and not is_console_api
|
is_webapp_api = request.path.startswith("/api/") and not is_console_api
|
||||||
|
|
||||||
if is_console_api or is_webapp_api:
|
if is_console_api or is_webapp_api:
|
||||||
if is_console_api:
|
if is_console_api:
|
||||||
@ -70,14 +71,13 @@ def create_flask_app_with_configs() -> DifyApp:
|
|||||||
try:
|
try:
|
||||||
# Check license status with caching (10 min TTL)
|
# Check license status with caching (10 min TTL)
|
||||||
license_status = EnterpriseService.get_cached_license_status()
|
license_status = EnterpriseService.get_cached_license_status()
|
||||||
if license_status in ["inactive", "expired", "lost"]:
|
if license_status in (LicenseStatus.INACTIVE, LicenseStatus.EXPIRED, LicenseStatus.LOST):
|
||||||
# Cookie clearing is handled by register_external_error_handlers
|
# Cookie clearing is handled by register_external_error_handlers
|
||||||
# in libs/external_api.py which detects the error code and calls
|
# in libs/external_api.py which detects the error code and calls
|
||||||
# build_force_logout_cookie_headers(). Frontend then checks
|
# build_force_logout_cookie_headers(). Frontend then checks
|
||||||
# code === 'unauthorized_and_force_logout' and calls location.reload().
|
# code === 'unauthorized_and_force_logout' and calls location.reload().
|
||||||
raise UnauthorizedAndForceLogout(
|
raise UnauthorizedAndForceLogout(
|
||||||
f"Enterprise license is {license_status}. "
|
f"Enterprise license is {license_status}. Please contact your administrator."
|
||||||
"Please contact your administrator."
|
|
||||||
)
|
)
|
||||||
except UnauthorizedAndForceLogout:
|
except UnauthorizedAndForceLogout:
|
||||||
raise
|
raise
|
||||||
|
|||||||
@ -101,10 +101,7 @@ class BaseRequest:
|
|||||||
# {"message": "..."}
|
# {"message": "..."}
|
||||||
# {"detail": "..."}
|
# {"detail": "..."}
|
||||||
error_message = (
|
error_message = (
|
||||||
error_data.get("message")
|
error_data.get("message") or error_data.get("error") or error_data.get("detail") or error_message
|
||||||
or error_data.get("error")
|
|
||||||
or error_data.get("detail")
|
|
||||||
or error_message
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
# If JSON parsing fails, use the default message
|
# If JSON parsing fails, use the default message
|
||||||
|
|||||||
@ -258,9 +258,11 @@ class EnterpriseService:
|
|||||||
info = cls.get_info()
|
info = cls.get_info()
|
||||||
license_info = info.get("License")
|
license_info = info.get("License")
|
||||||
if license_info:
|
if license_info:
|
||||||
status = license_info.get("status", "inactive")
|
from services.feature_service import LicenseStatus
|
||||||
|
|
||||||
|
status = license_info.get("status", LicenseStatus.INACTIVE)
|
||||||
# Only cache valid statuses so license updates are picked up immediately
|
# Only cache valid statuses so license updates are picked up immediately
|
||||||
if status in ("active", "expiring"):
|
if status in (LicenseStatus.ACTIVE, LicenseStatus.EXPIRING):
|
||||||
try:
|
try:
|
||||||
redis_client.setex(LICENSE_STATUS_CACHE_KEY, LICENSE_STATUS_CACHE_TTL, status)
|
redis_client.setex(LICENSE_STATUS_CACHE_KEY, LICENSE_STATUS_CACHE_TTL, status)
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -269,4 +271,4 @@ class EnterpriseService:
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Failed to get enterprise license status")
|
logger.exception("Failed to get enterprise license status")
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user