{"record":{"id":"db5be72516b8e641","repo":"windmill-labs/windmill","slug":"an-error-occurred-when-generating-csv","errorCode":null,"errorMessage":"An error occurred when generating CSV:","messagePattern":"An error occurred when generating CSV:","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/src/lib/components/table/tableUtils.ts","lineNumber":56,"sourceCode":"\t\t\t})\n\t\t\treturn {\n\t\t\t\t_id: nextId++,\n\t\t\t\trowData\n\t\t\t}\n\t\t})\n\t\treturn [hds, objs]\n\t} else {\n\t\treturn [[], []]\n\t}\n}\n\nexport function convertJsonToCsv(arr: Array<Record<string, any>>): string {\n\ttry {\n\t\tconst parser = new Parser({})\n\t\tconst csv = parser.parse(arr)\n\t\treturn csv\n\t} catch (err) {\n\t\tthrow new Error('An error occurred when generating CSV:' + err)\n\t}\n}\n","sourceCodeStart":38,"sourceCodeEnd":59,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/frontend/src/lib/components/table/tableUtils.ts#L38-L59","documentation":"convertJsonToCsv wraps the json-2-csv `Parser.parse` call and rethrows any parsing failure with this prefix. It is thrown when the input array cannot be serialized to CSV — typically malformed row objects, values the parser cannot handle, or a non-array input.","triggerScenarios":"Calling convertJsonToCsv(arr) where `arr` contains data the json-2-csv Parser cannot serialize (nested/circular structures it chokes on, invalid delimiters via the default config, or `arr` not being an array of records).","commonSituations":"Exporting table data to CSV from the result grid with unusual cell values (objects, Dates in odd shapes, nulls in key positions); passing undefined/empty or heterogeneous rows from an API response.","solutions":["Log the caught `err` (it is appended to the message) to see the exact parser complaint.","Sanitize/flatten rows before parsing: map values to primitives or strings (JSON.stringify nested objects).","Ensure the input is a non-empty array of plain objects with consistent keys.","Upgrade or pin the json-2-csv dependency if the failure is a known parser bug."],"exampleFix":"// before\nconst csv = convertJsonToCsv(rawRows) // may throw\n// after\nconst rows = rawRows.map((r) =>\n  Object.fromEntries(Object.entries(r).map(([k, v]) => [k, typeof v === 'object' ? JSON.stringify(v) : v]))\n)\nconst csv = convertJsonToCsv(rows)","handlingStrategy":"validation","validationCode":"function isCsvSafe(rows: unknown): rows is Array<Record<string, unknown>> {\n  return Array.isArray(rows) && rows.length > 0 &&\n    rows.every((r) => r !== null && typeof r === 'object' && !Array.isArray(r))\n}","typeGuard":"function isRecordArray(v: unknown): v is Array<Record<string, any>> {\n  return Array.isArray(v) && v.every((x) => typeof x === 'object' && x !== null && !Array.isArray(x))\n}","tryCatchPattern":"try {\n  const csv = convertJsonToCsv(rows)\n} catch (e) {\n  toast.error('CSV export failed: ' + (e as Error).message)\n  // fall back to JSON download\n  downloadJson(rows)\n}","preventionTips":["Flatten/serialize nested values (JSON.stringify) before export","Reject empty arrays and non-object rows up front","Read the appended `err` in the message — it names the exact parser complaint","Pin the json-2-csv version and check its changelog on upgrades"],"tags":["csv","serialization","data-export"],"backgroundTag":"csv-serialization-failed","analyzedSha":"e474e8803ce2ff5c2df09a58dab51d45f5c922ca","analyzedAt":"2026-09-03T12:38:19.024Z","contentChangedAt":"2026-09-03T12:38:19.024Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}