From fe940b3c6659c6775dec3837bb8d149c108a5866 Mon Sep 17 00:00:00 2001 From: MananTank Date: Thu, 9 Oct 2025 18:31:34 +0000 Subject: [PATCH] [BLD-395] Dashboard: Show error UI when parsing CSV fails (#8223) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ## PR-Codex overview This PR improves error handling in the `useCsvUpload` hook by adding a `try-catch` block around the CSV parsing logic. It ensures that if an error occurs during parsing or if the parsed data lacks an address, the component sets an error state and logs the error. ### Detailed summary - Introduced a `try-catch` block around the CSV parsing logic. - Added error handling to set `noCsv` to `true` if the parsed data's first entry does not have an `address`. - Ensured that any errors during parsing are logged to the console. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` ## Summary by CodeRabbit * **Bug Fixes** * Prevents crashes during CSV uploads by handling parse errors gracefully. * Ensures invalid or malformed CSV files are detected and flagged without updating data. * Maintains existing behavior for valid files, only updating data when required fields are present. * Improves reliability of the upload flow with clearer invalid-file handling. --- apps/dashboard/src/@/hooks/useCsvUpload.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/dashboard/src/@/hooks/useCsvUpload.ts b/apps/dashboard/src/@/hooks/useCsvUpload.ts index 66abe914310..aec90d8ddc3 100644 --- a/apps/dashboard/src/@/hooks/useCsvUpload.ts +++ b/apps/dashboard/src/@/hooks/useCsvUpload.ts @@ -119,12 +119,17 @@ export function useCsvUpload< } Papa.parse(csv, { complete: (results) => { - const data = props.csvParser(results.data as T[]); - if (!data[0]?.address) { + try { + const data = props.csvParser(results.data as T[]); + if (!data[0]?.address) { + setNoCsv(true); + return; + } + setRawData(data); + } catch (error) { + console.error(error); setNoCsv(true); - return; } - setRawData(data); }, header: true, });