mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 12:37:20 +08:00
fix: improve code quality in webhook services and controllers (#24634)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
parent
87abfbf515
commit
4ef6ff217e
@ -39,5 +39,5 @@ def handle_webhook(webhook_id: str):
|
|||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise NotFound(str(e))
|
raise NotFound(str(e))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(f"Webhook processing failed for {webhook_id}: {str(e)}")
|
logger.exception("Webhook processing failed for %s", webhook_id)
|
||||||
return jsonify({"error": "Internal server error", "message": str(e)}), 500
|
return jsonify({"error": "Internal server error", "message": str(e)}), 500
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
from collections.abc import Mapping
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from flask import request
|
from flask import request
|
||||||
@ -24,7 +25,7 @@ class WebhookService:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def get_webhook_trigger_and_workflow(
|
def get_webhook_trigger_and_workflow(
|
||||||
cls, webhook_id: str
|
cls, webhook_id: str
|
||||||
) -> tuple[WorkflowWebhookTrigger, Workflow, dict[str, Any]]:
|
) -> tuple[WorkflowWebhookTrigger, Workflow, Mapping[str, Any]]:
|
||||||
"""Get webhook trigger, workflow, and node configuration."""
|
"""Get webhook trigger, workflow, and node configuration."""
|
||||||
with Session(db.engine) as session:
|
with Session(db.engine) as session:
|
||||||
# Get webhook trigger
|
# Get webhook trigger
|
||||||
@ -118,14 +119,14 @@ class WebhookService:
|
|||||||
|
|
||||||
processed_files[name] = file_obj
|
processed_files[name] = file_obj
|
||||||
|
|
||||||
except Exception as e:
|
except Exception:
|
||||||
logger.exception(f"Failed to process file upload {name}: {str(e)}")
|
logger.exception("Failed to process file upload %s", name)
|
||||||
# Continue processing other files
|
# Continue processing other files
|
||||||
|
|
||||||
return processed_files
|
return processed_files
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_webhook_request(cls, webhook_data: dict[str, Any], node_config: dict[str, Any]) -> dict[str, Any]:
|
def validate_webhook_request(cls, webhook_data: dict[str, Any], node_config: Mapping[str, Any]) -> dict[str, Any]:
|
||||||
"""Validate webhook request against node configuration."""
|
"""Validate webhook request against node configuration."""
|
||||||
try:
|
try:
|
||||||
node_data = node_config.get("data", {})
|
node_data = node_config.get("data", {})
|
||||||
@ -176,9 +177,9 @@ class WebhookService:
|
|||||||
|
|
||||||
return {"valid": True}
|
return {"valid": True}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception:
|
||||||
logger.exception(f"Validation error: {str(e)}")
|
logger.exception("Validation error")
|
||||||
return {"valid": False, "error": f"Validation failed: {str(e)}"}
|
return {"valid": False, "error": "Validation failed"}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def trigger_workflow_execution(
|
def trigger_workflow_execution(
|
||||||
@ -198,7 +199,7 @@ class WebhookService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not tenant_owner:
|
if not tenant_owner:
|
||||||
logger.error(f"Tenant owner not found for tenant {webhook_trigger.tenant_id}")
|
logger.error("Tenant owner not found for tenant %s", webhook_trigger.tenant_id)
|
||||||
raise ValueError("Tenant owner not found")
|
raise ValueError("Tenant owner not found")
|
||||||
|
|
||||||
# Prepare inputs for the webhook node
|
# Prepare inputs for the webhook node
|
||||||
@ -228,12 +229,12 @@ class WebhookService:
|
|||||||
trigger_data,
|
trigger_data,
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception:
|
||||||
logger.exception(f"Failed to trigger workflow for webhook {webhook_trigger.webhook_id}: {str(e)}")
|
logger.exception("Failed to trigger workflow for webhook %s", webhook_trigger.webhook_id)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def generate_webhook_response(cls, node_config: dict[str, Any]) -> tuple[dict[str, Any], int]:
|
def generate_webhook_response(cls, node_config: Mapping[str, Any]) -> tuple[dict[str, Any], int]:
|
||||||
"""Generate HTTP response based on node configuration."""
|
"""Generate HTTP response based on node configuration."""
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user