From e471ac1851abb0f3da3db7d3d9cb93fc5a0cf0ee Mon Sep 17 00:00:00 2001 From: Chirantan Pradhan Date: Sat, 2 Aug 2025 18:15:43 +0530 Subject: [PATCH] Updated insert-row.mjs Enhanced Error Handling Wraps the parseRowValues call in try-catch Individual try-catch for each row insertion --- .../actions/insert-row/insert-row.mjs | 95 +++++++++++++++---- 1 file changed, 77 insertions(+), 18 deletions(-) diff --git a/components/postgresql/actions/insert-row/insert-row.mjs b/components/postgresql/actions/insert-row/insert-row.mjs index f09d8fc04a82e..a23393b146e0f 100644 --- a/components/postgresql/actions/insert-row/insert-row.mjs +++ b/components/postgresql/actions/insert-row/insert-row.mjs @@ -38,31 +38,90 @@ export default { table, rowValues, } = this; + + if (!schema || !table) { + throw new Error("Schema and table are required"); + } + + if (!rowValues) { + throw new Error("Row values are required"); + } + const results = []; - const parsedRowValues = parseRowValues(rowValues); + let parsedRowValues; + + try { + parsedRowValues = parseRowValues(rowValues); + } catch (error) { + throw new Error(`Invalid row values format: ${error.message}`); + } + + if (!parsedRowValues) { + throw new Error("Parsed row values cannot be null or undefined"); + } + const parsedRowValuesArray = Array.isArray(parsedRowValues) ? parsedRowValues - : [ - parsedRowValues, - ]; + : [parsedRowValues]; + + if (parsedRowValuesArray.length === 0) { + throw new Error("No valid rows to insert"); + } const errorMsg = "New row(s) not inserted due to an error. "; - for (const row of parsedRowValuesArray) { - const columns = Object.keys(row); - const values = Object.values(row); - const res = await this.postgresql.insertRow( - schema, - table, - columns, - values, - errorMsg, - ); - results.push(res); + + for (let i = 0; i < parsedRowValuesArray.length; i++) { + const row = parsedRowValuesArray[i]; + + try { + + if (!row || typeof row !== 'object') { + throw new Error(`Row ${i + 1} is not a valid object`); + } + + const columns = Object.keys(row); + const values = Object.values(row); + + if (columns.length === 0) { + throw new Error(`Row ${i + 1} cannot be empty`); + } + + console.log(`Inserting row ${i + 1}:`, { + schema, + table, + columns, + values: values.map(v => typeof v === 'string' && v.length > 100 ? `${v.substring(0, 100)}...` : v) + }); + + const res = await this.postgresql.insertRow( + schema, + table, + columns, + values, + errorMsg, + ); + + results.push(res); + console.log(`Successfully inserted row ${i + 1}`); + + } catch (error) { + console.error(`Failed to insert row ${i + 1}:`, { + row, + error: error.message, + stack: error.stack + }); + + + throw new Error(`${errorMsg}Row ${i + 1}: ${error.message}`); + } } - $.export("$summary", `Successfully inserted ${results.length} row${results.length === 1 - ? "" - : "s"}`); + + const summary = `Successfully inserted ${results.length} row${results.length === 1 ? "" : "s"}`; + console.log(summary); + $.export("$summary", summary); + return results; }, }; +