{"record":{"id":"316aae96ac21e41e","repo":"jackwener/OpenCLI","slug":"mdn-limit-must-be-a-positive-integer","errorCode":null,"errorMessage":"mdn limit must be a positive integer","messagePattern":"mdn limit must be a positive integer","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/mdn/search.js","lineNumber":23,"sourceCode":"// canonical MDN URL.\nimport { cli, Strategy } from '@jackwener/opencli/registry';\nimport { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';\n\nconst MDN_BASE = 'https://developer.mozilla.org';\nconst UA = 'opencli-mdn-adapter (+https://github.com/jackwener/opencli)';\nconst ALLOWED_LOCALES = new Set(['en-US', 'de', 'es', 'fr', 'ja', 'ko', 'pt-BR', 'ru', 'zh-CN', 'zh-TW']);\n\nfunction requireString(value, label) {\n    const s = String(value ?? '').trim();\n    if (!s) throw new ArgumentError(`mdn ${label} cannot be empty`);\n    return s;\n}\n\nfunction requireBoundedInt(value, defaultValue, maxValue) {\n    const raw = value ?? defaultValue;\n    const n = typeof raw === 'number' ? raw : Number(raw);\n    if (!Number.isInteger(n) || n <= 0) {\n        throw new ArgumentError('mdn limit must be a positive integer');\n    }\n    if (n > maxValue) {\n        throw new ArgumentError(`mdn limit must be <= ${maxValue}`);\n    }\n    return n;\n}\n\nfunction requireLocale(value) {\n    const s = String(value ?? 'en-US').trim();\n    if (!ALLOWED_LOCALES.has(s)) {\n        throw new ArgumentError(\n            `mdn locale \"${value}\" is not supported`,\n            `Allowed locales: ${[...ALLOWED_LOCALES].join(' / ')}`,\n        );\n    }\n    return s;\n}\n","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/mdn/search.js#L5-L41","documentation":"requireBoundedInt validates the limit parameter: it defaults the value, coerces to number, and requires a positive integer. If the value is not an integer or is <= 0, it throws this ArgumentError. The MDN search API requires a valid size parameter.","triggerScenarios":"Passing a limit of 0, a negative number, a non-numeric string (e.g. 'ten', ''), a float like 2.5, or NaN to the mdn search limit option.","commonSituations":"CLI flag typo (--limit abc), parsing limits from config files that hold strings like '5x', dividing values producing floats, forgetting that empty string coerces to 0.","solutions":["Pass a positive integer, e.g. --limit 10.","Check the value: Number.isInteger(Number(v)) && Number(v) > 0 before calling.","Fix config/env sources that supply non-numeric or zero limit values.","Omit the option to use the library default instead of an invalid value."],"exampleFix":"// before\nawait mdnSearch({ query: 'fetch api', limit: '5x' });\n// after\nconst limit = Number.parseInt(rawLimit, 10);\nawait mdnSearch({ query: 'fetch api', limit: Number.isInteger(limit) && limit > 0 ? limit : undefined });","handlingStrategy":"validation","validationCode":"const n = Number.parseInt(rawLimit, 10);\nif (!Number.isInteger(n) || n <= 0) throw new RangeError('limit must be a positive integer');","typeGuard":"function isPositiveInt(v) { return typeof v === 'number' && Number.isInteger(v) && v > 0; }","tryCatchPattern":"try { await mdnSearch({ query, limit }); } catch (e) { if (String(e.message).includes('limit must be a positive integer')) { limit = 10; return retry(); } throw e; }","preventionTips":["Parse CLI numbers with Number.parseInt and validate","Never pass raw strings from config as numeric options","Use the library default by omitting invalid limits"],"tags":["validation","argument-error","cli"],"backgroundTag":"invalid-positive-integer-parameter","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}