WaterCrawl crawler error responses can leak JSONDecodeError

在 Dify 中配置 WaterCrawl 网站抓取功能(Self Hosted Source / Docker),当 WaterCrawl API 或代理网关返回状态码 401、403 或 4xx 且响应体为 HTML/纯文本而非 JSON 时,触发 JSONDecodeError 。

WaterCrawl crawler error responses can leak JSONDecodeError

WaterCrawl crawler error responses can leak JSONDecodeError

快速结论:当 WaterCrawl 爬虫 API 返回非 JSON 格式的 401/403/4xx 响应体时(如 HTML 页面),异常处理类会因无保护的 response.json() 调用抛出 JSONDecodeError,掩盖原始业务异常。优先排查 WaterCrawl 配置或中间代理是否返回了 text/html 类型的错误响应。

问题场景

在 Dify 中配置 WaterCrawl 网站抓取功能(Self Hosted Source / Docker),当 WaterCrawl API 或代理网关返回状态码 401、403 或 4xx 且响应体为 HTML/纯文本而非 JSON 时,触发 JSONDecodeError

报错原文

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

原因分析

可能原因:

  • WaterCrawlBadRequestError.__init__(以及继承该类的 WaterCrawlAuthenticationErrorWaterCrawlPermissionError)在 exceptions.py 第 13 行无条件调用 response.json(),没有处理 JSON 解析异常。
  • client.py 第 124 行的 response.json() or {} 同样缺少错误保护。
  • 当 WaterCrawl、代理或上游网关返回非 JSON 体的 4xx 错误时,预期的 WaterCrawl 异常会被低层 JSONDecodeError 替换。

环境排查

  • 确认 Dify 版本是否为当前 main 分支(通过代码检查发现问题)。
  • 检查 WaterCrawl API 配置是否正确。
  • 检查是否有中间代理(如 Nginx、反向代理)返回 HTML 页面而非 JSON。

解决步骤

  1. 优先尝试:exceptions.py 中修改 WaterCrawlBadRequestError 类的 __init__ 方法,为 response.json() 添加 try/except,捕获 ValueErrorException,回退使用 response.text
class WaterCrawlBadRequestError(WaterCrawlError):
    def __init__(self, response):
        self.status_code = response.status_code
        self.response = response
        try:
            data = response.json()
        except (ValueError, Exception):
            data = {}
        self.message = data.get("message", response.text or "Unknown error occurred")
        self.errors = data.get("errors", {})
        super().__init__(self.message)
  1. client.py 第 124 行,将 return response.json() or {}try/except 包裹,回退到空字典或抛出清晰的错误信息。

验证方法

模拟一个返回 401 状态码且响应体为 HTML 格式的请求,确认触发时抛出的是 WaterCrawlAuthenticationError 而非 JSONDecodeError,且异常信息为响应文本或默认错误消息。

参考来源

langgenius/dify #37513

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 14580

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注