{"record":{"id":"a8d1a1bd9f9dbc72","repo":"jackwener/OpenCLI","slug":"commandname-malformed-comment-row-at-index-i","errorCode":null,"errorMessage":"${commandName}: malformed comment row at index ${index}","messagePattern":"(.+?): malformed comment row at index (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/xiaohongshu/comments.js","lineNumber":113,"sourceCode":"        if ((parsed.protocol !== 'https:' && parsed.protocol !== 'http:') || parsed.username || parsed.password) {\n            throw new CommandExecutionError(`${commandName}: malformed comment row image URL`);\n        }\n        const href = parsed.toString();\n        if (!urls.includes(href))\n            urls.push(href);\n    }\n    return urls;\n}\n\nexport function normalizeCommentRows(value, commandName = 'xiaohongshu/comments') {\n    if (value == null)\n        return [];\n    if (!Array.isArray(value)) {\n        throw new CommandExecutionError(`${commandName}: malformed comments payload`);\n    }\n    return value.map((row, index) => {\n        if (!row || typeof row !== 'object' || Array.isArray(row)) {\n            throw new CommandExecutionError(`${commandName}: malformed comment row at index ${index}`);\n        }\n        const text = normalizeOptionalString(row.text, 'text', commandName);\n        if (!text) {\n            throw new CommandExecutionError(`${commandName}: malformed comment row text`);\n        }\n        const likes = Number(row.likes);\n        if (!Number.isInteger(likes) || likes < 0) {\n            throw new CommandExecutionError(`${commandName}: malformed comment row likes`);\n        }\n        if (typeof row.is_reply !== 'boolean') {\n            throw new CommandExecutionError(`${commandName}: malformed comment row is_reply`);\n        }\n        return {\n            author: normalizeOptionalString(row.author, 'author', commandName),\n            authorHrefRaw: normalizeOptionalString(row.authorHrefRaw, 'authorHrefRaw', commandName),\n            text,\n            likes,\n            time: normalizeOptionalString(row.time, 'time', commandName),","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/xiaohongshu/comments.js#L95-L131","documentation":"Each element of the comments array must be a plain non-null object. Primitives, arrays, or null elements throw this error, which includes the failing index for debugging.","triggerScenarios":"The comments array contains null, a string, a number, or a nested array at some position - usually sparse data or a changed payload where some entries are placeholders.","commonSituations":"Deleted/filtered comments serialized as null, XHS returning partial rows, or callers concatenating mixed data into the array.","solutions":["Filter the array before normalizing: rows.filter(r => r && typeof r === 'object' && !Array.isArray(r)).","Log rows[index] reported in the error to identify the bad element.","Fix upstream extraction to skip placeholder/deleted comment entries.","Update the normalizer to silently drop invalid rows if graceful degradation is acceptable."],"exampleFix":"// before\nnormalizeCommentRows(rows, cmd);\n// after\nconst clean = rows.filter(r => r && typeof r === 'object' && !Array.isArray(r));\nnormalizeCommentRows(clean, cmd);","handlingStrategy":"validation","validationCode":"const clean = (rows ?? []).filter(r => r && typeof r === 'object' && !Array.isArray(r));\nif (clean.length !== rows?.length) console.warn(`Dropped ${rows.length - clean.length} invalid comment rows`);","typeGuard":"function isCommentRow(r) {\n  return Boolean(r) && typeof r === 'object' && !Array.isArray(r) && typeof r.text === 'string';\n}","tryCatchPattern":"try {\n  comments = normalizeCommentRows(rows, cmd);\n} catch (e) {\n  const m = /malformed comment row at index (\\d+)/.exec(e.message || '');\n  if (e instanceof CommandExecutionError && m) {\n    console.warn('Skipping bad row at index', m[1]);\n    rows.splice(Number(m[1]), 1);\n    comments = normalizeCommentRows(rows, cmd);\n  } else throw e;\n}","preventionTips":["Pre-filter null/placeholder rows (deleted comments) before normalizing.","Inspect the reported index to identify bad elements quickly.","Keep extraction code skipping non-object entries.","Validate with a schema (text string, likes number) before calling the API."],"tags":["validation","schema","scraping"],"backgroundTag":"schema-validation-failed","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}