]> git.ardis.lu - csvio/commitdiff
Add graceful failure example
authorArdis Lu <ardisglu@gmail.com>
Wed, 22 Jan 2025 05:35:38 +0000 (21:35 -0800)
committerArdis Lu <ardisglu@gmail.com>
Wed, 22 Jan 2025 05:35:38 +0000 (21:35 -0800)
examples/README.md
examples/data/ex9-in.csv [new file with mode: 0644]
examples/data/ex9-out.csv [new file with mode: 0644]
examples/ex9.js [new file with mode: 0644]

index 3d97d78a20db90194283d5f6292b45744937285a..f2000cd6c5d5fa2a47da40d51abcc2167f6d0b71 100644 (file)
@@ -24,4 +24,6 @@ node ./examples/ex1.js
 
 [Example 7: Processing rows in batches (multi-row input)](./ex7.js)
 
-[Example 8: Processing rows concurrently (async)](./ex8.js)
\ No newline at end of file
+[Example 8: Processing rows concurrently (async)](./ex8.js)
+
+[Example 9: Handling a flaky transformation (graceful failure)](./ex9.js)
\ No newline at end of file
diff --git a/examples/data/ex9-in.csv b/examples/data/ex9-in.csv
new file mode 100644 (file)
index 0000000..9e3c659
--- /dev/null
@@ -0,0 +1,11 @@
+columnA,columnB\r
+1,A\r
+2,B\r
+3,C\r
+4,D\r
+5,E\r
+6,F\r
+7,G\r
+8,H\r
+9,I\r
+10,J\r
diff --git a/examples/data/ex9-out.csv b/examples/data/ex9-out.csv
new file mode 100644 (file)
index 0000000..c1979c5
--- /dev/null
@@ -0,0 +1,11 @@
+columnA,columnB\r
+1,A\r
+n/a,n/a\r
+3,C\r
+n/a,n/a\r
+5,E\r
+n/a,n/a\r
+7,G\r
+n/a,n/a\r
+9,I\r
+n/a,n/a\r
diff --git a/examples/ex9.js b/examples/ex9.js
new file mode 100644 (file)
index 0000000..1cbfba3
--- /dev/null
@@ -0,0 +1,22 @@
+// ## Example 9: Handling a flaky transformation (graceful failure)
+
+import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+
+// Set a placeholder value and continue processing
+function graceful(row) {
+  return row.map(() => 'n/a');
+}
+
+// Will throw on every other row
+let i = 0;
+function flaky(row) {
+  i++;
+  if (i % 2 === 0) {
+    throw new Error('Example error message');
+  }
+  return row;
+}
+
+await new CSVReader(new URL('./data/ex9-in.csv', import.meta.url))
+  .pipeThrough(new CSVTransformer(flaky, { onError: row => graceful(row) }))
+  .pipeTo(new CSVWriter(new URL('./data/ex9-out.csv', import.meta.url)));
\ No newline at end of file
This page took 0.063579 seconds and 4 git commands to generate.