{"record":{"id":"74759b66eb128dc2","repo":"sansan0/TrendRadar","slug":"limit","errorCode":null,"errorMessage":"limit 参数必须是整数类型","messagePattern":"limit 参数必须是整数类型","errorType":"validation","errorClass":"InvalidParameterError","httpStatus":null,"severity":"error","filePath":"mcp_server/utils/validators.py","lineNumber":280,"sourceCode":"        limit: 限制数量（整数或字符串）\n        default: 默认值\n        max_limit: 最大限制\n\n    Returns:\n        验证后的限制值\n\n    Raises:\n        InvalidParameterError: 参数无效\n    \"\"\"\n    if limit is None:\n        return default\n\n    # 支持字符串形式的整数（某些 MCP 客户端会将数字序列化为字符串）\n    if isinstance(limit, str):\n        limit = _parse_string_to_int(limit, \"limit\")\n\n    if not isinstance(limit, int):\n        raise InvalidParameterError(\"limit 参数必须是整数类型\")\n\n    if limit <= 0:\n        raise InvalidParameterError(\"limit 必须大于0\")\n\n    if limit > max_limit:\n        raise InvalidParameterError(\n            f\"limit 不能超过 {max_limit}\",\n            suggestion=f\"请使用分页或降低limit值\"\n        )\n\n    return limit\n\n\ndef validate_date(date_str: str) -> datetime:\n    \"\"\"\n    验证日期格式\n\n    Args:","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/sansan0/TrendRadar/blob/8ee26026ba6c11dec41a95fb3895a7162876caa1/mcp_server/utils/validators.py#L262-L298","documentation":"Raised by validate_limit when, after the string-to-int coercion step, the value is still not an int. Because strings are already converted by _parse_string_to_int (which raises its own, more specific error for bad strings), this branch catches non-string, non-int types: floats like 3.7 (isinstance(3.7, int) is False), bools pass but floats don't, dicts, lists, None is handled earlier.","triggerScenarios":"Passing limit as a JSON float {\"limit\": 20.0} (some clients emit 20.0 instead of 20), a dict, a list, or a bool. Note isinstance(True, int) is True in Python, so true is accepted as 1 and won't hit this.","commonSituations":"JavaScript/TypeScript clients where a computed limit becomes a float (e.g. avg, division); JSON encoders emitting .0 for whole numbers; accidentally nesting the parameter {\"limit\": {\"value\": 20}}.","solutions":["Send an integer literal: 20, not 20.0","In JS, round before sending: Math.trunc(limit) or limit | 0","If the value is computed, coerce client-side with parseInt/Number.isInteger checks","Catch InvalidParameterError and retry with a sane default (e.g. 20)"],"exampleFix":"// before\n{\"limit\": 20.5}\n// after\n{\"limit\": 20}","handlingStrategy":"type-guard","validationCode":"const limit = Number.isInteger(rawLimit) ? rawLimit : Math.trunc(Number(rawLimit));","typeGuard":"const isInt = (v: unknown): v is number => typeof v === 'number' && Number.isInteger(v);","tryCatchPattern":"try { call({ limit }); }\ncatch (e) {\n  if (/limit 参数必须是整数类型/.test(e.message)) call({ limit: Math.trunc(limit) });\n  else throw e;\n}","preventionTips":["Apply Math.trunc to computed limits in JS pipelines","Never send 20.0-style floats for integer params (avoid float division when computing limits)","Type the field as integer in your client models"],"tags":["validation","limit","type-check","json"],"backgroundTag":null,"analyzedSha":"8ee26026ba6c11dec41a95fb3895a7162876caa1","analyzedAt":"2026-08-15T01:42:18.084Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}