{"record":{"id":"22d866b1108b585e","repo":"jackwener/OpenCLI","slug":"wikipedia-lang-must-be-a-language-code-like-en-zh","errorCode":null,"errorMessage":"wikipedia lang must be a language code like en, zh, ja (got \"${args.lang}\")","messagePattern":"wikipedia lang must be a language code like en, zh, ja \\(got \"(.+?)\"\\)","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/wikipedia/page.js","lineNumber":34,"sourceCode":"    access: 'read',\n    description: 'Full plain-text extract of a Wikipedia article (optional paragraph cap).',\n    domain: 'wikipedia.org',\n    strategy: Strategy.PUBLIC,\n    browser: false,\n    args: [\n        { name: 'title', positional: true, required: true, type: 'string', help: 'Article title (e.g. \"Transformer (machine learning model)\")' },\n        { name: 'lang', type: 'string', default: 'en', help: 'Language code (en, zh, ja, de, ...).' },\n        { name: 'paragraphs', type: 'int', default: 0, help: 'Cap to first N paragraphs (0 = full article).' },\n    ],\n    columns: ['title', 'description', 'pageId', 'paragraphs', 'extract', 'url'],\n    func: async (args) => {\n        const title = String(args.title ?? '').trim();\n        if (!title) {\n            throw new ArgumentError('wikipedia page title cannot be empty');\n        }\n        const lang = String(args.lang ?? 'en').trim().toLowerCase();\n        if (!/^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(lang)) {\n            throw new ArgumentError(`wikipedia lang must be a language code like en, zh, ja (got \"${args.lang}\")`);\n        }\n        const paragraphsCap = Number(args.paragraphs ?? 0);\n        if (!Number.isInteger(paragraphsCap) || paragraphsCap < 0) {\n            throw new ArgumentError('paragraphs must be a non-negative integer (0 = full article)');\n        }\n\n        const url = new URL(`https://${lang}.wikipedia.org/w/api.php`);\n        url.searchParams.set('action', 'query');\n        url.searchParams.set('format', 'json');\n        url.searchParams.set('formatversion', '2');\n        url.searchParams.set('prop', 'extracts|info|description');\n        url.searchParams.set('inprop', 'url');\n        url.searchParams.set('explaintext', '1');\n        url.searchParams.set('redirects', '1');\n        url.searchParams.set('titles', title);\n\n        let resp;\n        try {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/wikipedia/page.js#L16-L52","documentation":"The lang argument must match /^[a-z]{2,3}(?:-[a-z0-9]+)?$/ after lowercase/trim, because it is interpolated directly into the hostname https://<lang>.wikipedia.org. If it does not look like an ISO language code (optionally with a region suffix), an ArgumentError is thrown before any fetch.","triggerScenarios":"Passing a full locale like 'en_US' or 'pt-BR' with underscore/uppercase; passing a display name like 'english' or 'Chinese'; passing an empty or numeric lang value; casing like 'EN' (fixed by trim/lowercase, but 'en-US' is fine while 'eng-US' is not).","commonSituations":"Deriving lang from an OS locale (e.g. en_US.UTF-8) without normalizing; users typing language names instead of codes; i18n frameworks exporting BCP-47 tags with script subtags (zh-Hans fails here).","solutions":["Pass a 2-3 letter lowercase ISO 639 code, optionally with a short region suffix (en, zh, ja, zh-cn)","Normalize OS locales: strip encoding and convert _ to - and lowercase before passing","Omit lang entirely to use the default 'en'"],"exampleFix":"// before\nconst lang = process.env.LANG; // \"en_US.UTF-8\"\nawait run(['wikipedia', 'page', title, '--lang', lang]);\n// after\nconst lang = (process.env.LANG || 'en').split('.')[0].replace('_', '-').toLowerCase(); // \"en-us\" — or map to \"en\"\nawait run(['wikipedia', 'page', title, '--lang', lang]);","handlingStrategy":"validation","validationCode":"const lang = String(rawLang ?? 'en').trim().toLowerCase();\nif (!/^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(lang)) throw new Error(`bad lang: ${rawLang}`);","typeGuard":"function isWikiLang(v) { return typeof v === 'string' && /^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(v.trim().toLowerCase()); }","tryCatchPattern":"try {\n  return await pageCommand({ ...args, lang: normalizedLang });\n} catch (err) {\n  if (err instanceof ArgumentError && err.message.includes('lang')) {\n    console.error(`invalid lang \"${args.lang}\"; using \"en\"`);\n    return await pageCommand({ ...args, lang: 'en' });\n  }\n  throw err;\n}","preventionTips":["Normalize OS locales (strip encoding, '_'→'-', lowercase) before passing","Use ISO 639 codes (en, zh, ja), never language names","Drop script subtags like zh-Hans → zh or zh-cn","Rely on the default 'en' when unsure"],"tags":["argument-error","validation","wikipedia","language-code"],"backgroundTag":"invalid-language-code","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}