mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 04:26:30 +08:00
fix: add responding error information when obtain pipeline template detail failed (#33628)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
296b7044af
commit
dc69f65b4b
@ -46,6 +46,8 @@ class PipelineTemplateDetailApi(Resource):
|
|||||||
type = request.args.get("type", default="built-in", type=str)
|
type = request.args.get("type", default="built-in", type=str)
|
||||||
rag_pipeline_service = RagPipelineService()
|
rag_pipeline_service = RagPipelineService()
|
||||||
pipeline_template = rag_pipeline_service.get_pipeline_template_detail(template_id, type)
|
pipeline_template = rag_pipeline_service.get_pipeline_template_detail(template_id, type)
|
||||||
|
if pipeline_template is None:
|
||||||
|
return {"error": "Pipeline template not found from upstream service."}, 404
|
||||||
return pipeline_template, 200
|
return pipeline_template, 200
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,8 @@ class RemotePipelineTemplateRetrieval(PipelineTemplateRetrievalBase):
|
|||||||
Retrieval recommended app from dify official
|
Retrieval recommended app from dify official
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def get_pipeline_template_detail(self, template_id: str):
|
def get_pipeline_template_detail(self, template_id: str) -> dict | None:
|
||||||
|
result: dict | None
|
||||||
try:
|
try:
|
||||||
result = self.fetch_pipeline_template_detail_from_dify_official(template_id)
|
result = self.fetch_pipeline_template_detail_from_dify_official(template_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -35,17 +36,23 @@ class RemotePipelineTemplateRetrieval(PipelineTemplateRetrievalBase):
|
|||||||
return PipelineTemplateType.REMOTE
|
return PipelineTemplateType.REMOTE
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def fetch_pipeline_template_detail_from_dify_official(cls, template_id: str) -> dict | None:
|
def fetch_pipeline_template_detail_from_dify_official(cls, template_id: str) -> dict:
|
||||||
"""
|
"""
|
||||||
Fetch pipeline template detail from dify official.
|
Fetch pipeline template detail from dify official.
|
||||||
:param template_id: Pipeline ID
|
|
||||||
:return:
|
:param template_id: Pipeline template ID
|
||||||
|
:return: Template detail dict
|
||||||
|
:raises ValueError: When upstream returns a non-200 status code
|
||||||
"""
|
"""
|
||||||
domain = dify_config.HOSTED_FETCH_PIPELINE_TEMPLATES_REMOTE_DOMAIN
|
domain = dify_config.HOSTED_FETCH_PIPELINE_TEMPLATES_REMOTE_DOMAIN
|
||||||
url = f"{domain}/pipeline-templates/{template_id}"
|
url = f"{domain}/pipeline-templates/{template_id}"
|
||||||
response = httpx.get(url, timeout=httpx.Timeout(10.0, connect=3.0))
|
response = httpx.get(url, timeout=httpx.Timeout(10.0, connect=3.0))
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
return None
|
raise ValueError(
|
||||||
|
"fetch pipeline template detail failed,"
|
||||||
|
+ f" status_code: {response.status_code},"
|
||||||
|
+ f" response: {response.text[:1000]}"
|
||||||
|
)
|
||||||
data: dict = response.json()
|
data: dict = response.json()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|||||||
@ -117,13 +117,21 @@ class RagPipelineService:
|
|||||||
def get_pipeline_template_detail(cls, template_id: str, type: str = "built-in") -> dict | None:
|
def get_pipeline_template_detail(cls, template_id: str, type: str = "built-in") -> dict | None:
|
||||||
"""
|
"""
|
||||||
Get pipeline template detail.
|
Get pipeline template detail.
|
||||||
|
|
||||||
:param template_id: template id
|
:param template_id: template id
|
||||||
:return:
|
:param type: template type, "built-in" or "customized"
|
||||||
|
:return: template detail dict, or None if not found
|
||||||
"""
|
"""
|
||||||
if type == "built-in":
|
if type == "built-in":
|
||||||
mode = dify_config.HOSTED_FETCH_PIPELINE_TEMPLATES_MODE
|
mode = dify_config.HOSTED_FETCH_PIPELINE_TEMPLATES_MODE
|
||||||
retrieval_instance = PipelineTemplateRetrievalFactory.get_pipeline_template_factory(mode)()
|
retrieval_instance = PipelineTemplateRetrievalFactory.get_pipeline_template_factory(mode)()
|
||||||
built_in_result: dict | None = retrieval_instance.get_pipeline_template_detail(template_id)
|
built_in_result: dict | None = retrieval_instance.get_pipeline_template_detail(template_id)
|
||||||
|
if built_in_result is None:
|
||||||
|
logger.warning(
|
||||||
|
"pipeline template retrieval returned empty result, template_id: %s, mode: %s",
|
||||||
|
template_id,
|
||||||
|
mode,
|
||||||
|
)
|
||||||
return built_in_result
|
return built_in_result
|
||||||
else:
|
else:
|
||||||
mode = "customized"
|
mode = "customized"
|
||||||
|
|||||||
@ -59,6 +59,44 @@ class TestPipelineTemplateDetailApi:
|
|||||||
assert status == 200
|
assert status == 200
|
||||||
assert response == template
|
assert response == template
|
||||||
|
|
||||||
|
def test_get_returns_404_when_template_not_found(self, app):
|
||||||
|
api = PipelineTemplateDetailApi()
|
||||||
|
method = unwrap(api.get)
|
||||||
|
|
||||||
|
service = MagicMock()
|
||||||
|
service.get_pipeline_template_detail.return_value = None
|
||||||
|
|
||||||
|
with (
|
||||||
|
app.test_request_context("/?type=built-in"),
|
||||||
|
patch(
|
||||||
|
"controllers.console.datasets.rag_pipeline.rag_pipeline.RagPipelineService",
|
||||||
|
return_value=service,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
response, status = method(api, "non-existent-id")
|
||||||
|
|
||||||
|
assert status == 404
|
||||||
|
assert "error" in response
|
||||||
|
|
||||||
|
def test_get_returns_404_for_customized_type_not_found(self, app):
|
||||||
|
api = PipelineTemplateDetailApi()
|
||||||
|
method = unwrap(api.get)
|
||||||
|
|
||||||
|
service = MagicMock()
|
||||||
|
service.get_pipeline_template_detail.return_value = None
|
||||||
|
|
||||||
|
with (
|
||||||
|
app.test_request_context("/?type=customized"),
|
||||||
|
patch(
|
||||||
|
"controllers.console.datasets.rag_pipeline.rag_pipeline.RagPipelineService",
|
||||||
|
return_value=service,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
response, status = method(api, "non-existent-id")
|
||||||
|
|
||||||
|
assert status == 404
|
||||||
|
assert "error" in response
|
||||||
|
|
||||||
|
|
||||||
class TestCustomizedPipelineTemplateApi:
|
class TestCustomizedPipelineTemplateApi:
|
||||||
def test_patch_success(self, app):
|
def test_patch_success(self, app):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user