sansan0/TrendRadar · error · InvalidParameterError

日期表达式不能为空

Error message

日期表达式不能为空

What it means

Entry guard of DateParser.resolve_date_range_expression, the MCP-side helper that turns natural-language range expressions (本周, 最近7天, last week) into {start, end} dicts. It rejects falsy or non-string input before consulting the RANGE_EXPRESSIONS table or the dynamic "最近N天"/"last N days" patterns.

Source

Thrown at mcp_server/utils/date_parser.py:371

                    "start": "2025-11-18",
                    "end": "2025-11-24"
                },
                "current_date": "2025-11-26",
                "description": "本周(周一到周日)"
            }

        Raises:
            InvalidParameterError: 无法识别的日期表达式

        Examples:
            >>> DateParser.resolve_date_range_expression("本周")
            {"success": True, "date_range": {"start": "2025-11-18", "end": "2025-11-24"}, ...}

            >>> DateParser.resolve_date_range_expression("最近7天")
            {"success": True, "date_range": {"start": "2025-11-20", "end": "2025-11-26"}, ...}
        """
        if not expression or not isinstance(expression, str):
            raise InvalidParameterError(
                "日期表达式不能为空",
                suggestion="请提供有效的日期表达式,如:本周、最近7天、last week"
            )

        expression_lower = expression.strip().lower()
        today = datetime.now()
        today_str = today.strftime("%Y-%m-%d")

        # 1. 尝试匹配预定义表达式
        normalized = DateParser.RANGE_EXPRESSIONS.get(expression_lower)

        # 2. 尝试匹配动态 "最近N天" / "last N days" 模式
        if not normalized:
            # 中文: 最近N天
            cn_match = re.match(r'最近(\d+)天', expression_lower)
            if cn_match:
                days = int(cn_match.group(1))
                normalized = f"last_{days}_days"

View on GitHub (pinned to 8ee26026ba)

Solutions

  1. Pass a non-empty expression string: 本周, 上周, 最近7天, last week, this month, etc.
  2. Default missing parameters to a sensible expression (e.g. "最近7天") at the call site
  3. If you already have concrete dates, bypass this helper and supply the date range directly to the consuming tool

Example fix

# before
DateParser.resolve_date_range_expression(params.get("date_expression"))  # may be None

# after
DateParser.resolve_date_range_expression(params.get("date_expression") or "最近7天")
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(expression, str) or not expression.strip():
    expression = "最近7天"  # or raise a clear local error

Type guard

def is_range_expression(v) -> bool:
    return isinstance(v, str) and len(v.strip()) > 0

Try / catch

try:
    rng = DateParser.resolve_date_range_expression(expr)
except InvalidParameterError as e:
    if "不能为空" in str(e):
        rng = DateParser.resolve_date_range_expression("最近7天")
    else:
        raise

Prevention

When it happens

Trigger: Calling resolve_date_range_expression(None), "", or a non-string (dict/date/int); tool wiring that forwards an absent optional expression parameter.

Common situations: MCP tool schemas marking date_expression optional while the implementation requires it; LLM calls omitting the argument; passing an already-resolved date_range dict where an expression string was expected.

Related errors


AI-assisted analysis of sansan0/TrendRadar@8ee26026ba (2026-08-15). Data as JSON: /api/errors/3a4c4781c94af667. Report an issue: GitHub.