{"record":{"id":"6117a016f94e72e6","repo":"jackwener/OpenCLI","slug":"month-1-12","errorCode":null,"errorMessage":"月份非法：${month}，应为 1-12","messagePattern":"月份非法：(.+?)，应为 1-12","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/mubu/notes.js","lineNumber":24,"sourceCode":"\nfunction localToday() {\n  const d = new Date();\n  return { year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate() };\n}\n\nfunction lastDayOfMonth(year, month) {\n  return new Date(year, month, 0).getDate();\n}\n\nfunction validateYear(year, label = '年份') {\n  if (!Number.isInteger(year) || year < 1) {\n    throw new ArgumentError(`${label} 非法：${year}，应为正整数`);\n  }\n}\n\nfunction validateMonth(month) {\n  if (!Number.isInteger(month) || month < 1 || month > 12) {\n    throw new ArgumentError(`月份非法：${month}，应为 1-12`);\n  }\n}\n\nfunction validateDay(year, month, day) {\n  const maxDay = lastDayOfMonth(year, month);\n  if (!Number.isInteger(day) || day < 1 || day > maxDay) {\n    throw new ArgumentError(`日期非法：${year}-${month}-${day}（${year} 年 ${month} 月共 ${maxDay} 天）`);\n  }\n}\n\nfunction parseDate(s) {\n  const parts = s.split('-').map(Number);\n  if (parts.length !== 3 || parts.some(isNaN)) {\n    throw new ArgumentError(`日期格式错误：${s}，应为 YYYY-MM-DD`);\n  }\n  const [year, month, day] = parts;\n  validateYear(year);\n  validateMonth(month);","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/mubu/notes.js#L6-L42","documentation":"validateMonth guards that a month is an integer in 1-12 before any date math is done. The mubu notes CLI throws ArgumentError whenever a month value fails this check, so downstream date ranges are never built from nonsense months.","triggerScenarios":"Calling parseDate('2024-13-01') or parseMonth('2024-00'), or passing month=2.5 or a string month through any code path that reaches validateMonth.","commonSituations":"Users type malformed --from/--to values, scripts produce 0-based months (JS getMonth()), or locales emit MM values >12 from misparsed strings.","solutions":["Correct the month value to an integer 1-12.","If the month came from new Date().getMonth(), add 1 (JS months are 0-based).","Validate user input with a regex like /^\\d{4}-\\d{2}(-\\d{2})?$/ before calling the parser."],"exampleFix":"// before\nawait notes({ from: '2024-13-01' });\n// after\nawait notes({ from: '2024-12-01' });","handlingStrategy":"validation","validationCode":"function isValidMonth(m) { return Number.isInteger(m) && m >= 1 && m <= 12; }\nif (!isValidMonth(month)) throw new Error(`month must be 1-12, got ${month}`);","typeGuard":"const isValidMonth = (m) => Number.isInteger(m) && m >= 1 && m <= 12;","tryCatchPattern":"try {\n  const d = parseDate(input);\n} catch (e) {\n  if (/月份非法/.test(e.message)) console.error('Fix the month (1-12):', e.message);\n  else throw e;\n}","preventionTips":["Use date strings rather than raw month numbers so the parser validates everything.","Remember JS getMonth() is 0-based — always add 1.","Regex-validate YYYY-MM-DD input before parsing."],"tags":["date-validation","argument-error","cli"],"backgroundTag":"invalid-date-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}