{"record":{"id":"ace932234050096d","repo":"jackwener/OpenCLI","slug":"gmail-returned-a-malformed-label-at-index-index","errorCode":null,"errorMessage":"Gmail returned a malformed label at index ${index}","messagePattern":"Gmail returned a malformed label at index (.+?)","errorType":"error_code","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/gmail/utils.js","lineNumber":187,"sourceCode":"    if (!id) continue;\n    result.set(id, {\n      unread: Number.isFinite(Number(row?.[1])) ? Number(row[1]) : null,\n      total: Number.isFinite(Number(row?.[2])) ? Number(row[2]) : null,\n    });\n  }\n  return result;\n}\n\nexport function parseLabels(body) {\n  if (!Array.isArray(body) || body.length !== 19) {\n    throw new CommandExecutionError('Gmail batch-view response had an unexpected shape');\n  }\n  const counts = labelCounts(body[6]);\n  const rows = Array.isArray(body[1]) ? body[1] : [];\n  return rows.map((wrapper, index) => {\n    const record = Array.isArray(wrapper?.[0]) ? wrapper[0] : null;\n    const id = cleanString(record?.[0]);\n    if (!id) throw new CommandExecutionError(`Gmail returned a malformed label at index ${index}`);\n    const count = counts.get(id) || {};\n    return {\n      id,\n      name: cleanString(record?.[1]) || id,\n      type: id.startsWith('^x_') ? 'user' : 'system',\n      unreadCount: count.unread ?? null,\n      totalCount: count.total ?? null,\n    };\n  });\n}\n\nfunction senderFromRecord(record) {\n  const card = Array.isArray(record?.[10]) ? record[10] : null;\n  const address = cleanString(card?.[16]);\n  if (!address.includes('@')) return null;\n  return { address, name: cleanString(card?.[14]) || null };\n}\n","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/gmail/utils.js#L169-L205","documentation":"In parseLabels, each row of body[1] should be a wrapper whose [0] is a label record starting with a label id at index 0. If cleanString(record?.[0]) is empty, this CommandExecutionError is thrown with the row index. A label without an id cannot be used in queries or as a target, so the parser rejects the row.","triggerScenarios":"A /i/bv labels payload containing a row whose label record is missing or has an empty id at index 0 — placeholder rows during live sync, or Gmail changing the per-label record layout.","commonSituations":"Users creating/deleting labels in another session while gmail labels runs, Gmail A/B experiments changing record field order, partially loaded label lists.","solutions":["Retry — transient placeholder rows typically resolve once Gmail finishes syncing labels.","Update opencli if Gmail changed the label record layout.","Reload Gmail so the labels settings data loads completely, then rerun.","Avoid editing labels in another tab/session while running the command."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"null","typeGuard":"function isLabelRecord(wrapper) {\n  const record = Array.isArray(wrapper?.[0]) ? wrapper[0] : null;\n  return typeof record?.[0] === 'string' && record[0].trim().length > 0;\n}","tryCatchPattern":"try {\n  labels = await gmailLabels();\n} catch (error) {\n  if (String(error.message).includes('malformed label at index')) {\n    await sleep(1500); // let label sync placeholders settle\n    labels = await gmailLabels();\n  } else throw error;\n}","preventionTips":["Don't create/rename/delete labels in another session while listing labels.","Retry once — transient placeholder rows are the usual cause.","Hard-reload Gmail if labels were recently modified so settings data loads fully.","Report persistent failures with the label list state for parser updates."],"tags":["gmail","labels","malformed-row","schema-validation"],"backgroundTag":"unexpected-response-shape","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}