{"record":{"id":"b07416ed894c2bd8","repo":"jackwener/OpenCLI","slug":"page-must-be-an-integer-between-articles-min-p","errorCode":null,"errorMessage":"--page must be an integer between ${ARTICLES_MIN_PAGE} and ${ARTICLES_MAX_PAGE}, got ${JSON.stringify(raw)}","messagePattern":"--page must be an integer between (.+?) and (.+?), got (.+?)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/toutiao/utils.js","lineNumber":17,"sourceCode":"/**\n * Shared helpers for the toutiao adapter.\n */\nimport { ArgumentError, CommandExecutionError } from '@jackwener/opencli/errors';\n\nconst ARTICLES_MIN_PAGE = 1;\nconst ARTICLES_MAX_PAGE = 4;\nconst HOT_MIN_LIMIT = 1;\nconst HOT_MAX_LIMIT = 50;\nconst RECOMMEND_MIN_LIMIT = 1;\nconst RECOMMEND_MAX_LIMIT = 50;\n\nexport function parseArticlesPage(raw, fallback = 1) {\n    if (raw === undefined || raw === null || raw === '') return fallback;\n    const parsed = Number(raw);\n    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {\n        throw new ArgumentError(`--page must be an integer between ${ARTICLES_MIN_PAGE} and ${ARTICLES_MAX_PAGE}, got ${JSON.stringify(raw)}`);\n    }\n    if (parsed < ARTICLES_MIN_PAGE || parsed > ARTICLES_MAX_PAGE) {\n        throw new ArgumentError(`--page must be between ${ARTICLES_MIN_PAGE} and ${ARTICLES_MAX_PAGE}, got ${parsed}`);\n    }\n    return parsed;\n}\n\nexport function parseRecommendLimit(raw, fallback = 20) {\n    if (raw === undefined || raw === null || raw === '') return fallback;\n    const parsed = Number(raw);\n    if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) {\n        throw new ArgumentError(`--limit must be an integer between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${JSON.stringify(raw)}`);\n    }\n    if (parsed < RECOMMEND_MIN_LIMIT || parsed > RECOMMEND_MAX_LIMIT) {\n        throw new ArgumentError(`--limit must be between ${RECOMMEND_MIN_LIMIT} and ${RECOMMEND_MAX_LIMIT}, got ${parsed}`);\n    }\n    return parsed;\n}","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/toutiao/utils.js#L1-L35","documentation":"This ArgumentError is thrown by parseArticlesPage when the --page value is provided but is not a finite integer (e.g. 'abc', '1.5', 'NaN'). The error distinguishes type failures (this check) from range failures (the next check), and includes the raw JSON-stringified input for diagnosis.","triggerScenarios":"Running an article command with `--page abc`, `--page 1.5`, `--page ''` is fine (falls back) but `--page 0x`, or any non-numeric/decimal string passed programmatically to parseArticlesPage.","commonSituations":"Typo in the CLI flag value ('page 2a'); copy-pasting a value with stray characters; scripting errors passing a float or string instead of an integer; locale-formatted numbers ('２' full-width).","solutions":["Pass a plain integer, e.g. `--page 2`.","Remove stray whitespace/characters from the value.","In scripts, coerce with Math.trunc(Number(x)) after validating with Number.isInteger before calling.","Omit --page entirely to use the default page (fallback 1)."],"exampleFix":"// before\ntoutiao articles --page 1.5\n// after\ntoutiao articles --page 2","handlingStrategy":"validation","validationCode":"function validatePage(raw) {\n  if (raw === undefined || raw === null || raw === '') return; // falls back to default\n  const n = Number(raw);\n  if (!Number.isInteger(n)) throw new TypeError(`--page must be an integer, got ${JSON.stringify(raw)}`);\n}","typeGuard":"function isIntegerPage(v) {\n  return typeof v === 'number' && Number.isInteger(v);\n}","tryCatchPattern":"try {\n  await articles({ page: rawPage });\n} catch (e) {\n  if (e instanceof ArgumentError && e.message.startsWith('--page must be an integer')) {\n    console.error(`Invalid --page value; use e.g. --page 1`);\n    process.exitCode = 2;\n    return;\n  }\n  throw e;\n}","preventionTips":["Always pass bare integers to --page (no units, decimals, or separators).","Validate user input with Number.isInteger before invoking commands from scripts.","Omit --page to accept the default rather than guessing a value.","Quote shell arguments to avoid stray characters being appended."],"tags":["argument-validation","cli","input-error"],"backgroundTag":"invalid-argument-type","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}