]> git.ardis.lu - csvio/commitdiff
Add skip error strategy
authorArdis Lu <ardisglu@gmail.com>
Thu, 6 Mar 2025 06:55:39 +0000 (22:55 -0800)
committerArdis Lu <ardisglu@gmail.com>
Thu, 6 Mar 2025 06:55:39 +0000 (22:55 -0800)
src/errorStrategies.js
test/errorStrategies.test.js

index b9a24a2f18a2317285fd1a8cbe916776bafb8a4e..90a73bcfac06c32f0de70312671cd31f2778ce02 100644 (file)
@@ -13,6 +13,17 @@ async function sleep(ms) {
 }
 export const utils = { sleep };
 
+/**
+ * Create a `TransformationErrorFunction` that handles errors by silently swallowing the error and skipping the output
+ * row.
+ * 
+ * @returns {TransformationErrorFunction} A `TransformationErrorFunction` that may be passed to a `CSVTransformer`'s
+ * `onError` option.
+ */
+function skip() {
+  return () => null;
+}
+
 /**
  * Create a `TransformationErrorFunction` that handles errors by filling the output row with a given placeholder value.
  * 
@@ -100,5 +111,5 @@ function backoff(iterations, options = {}) {
   }
 }
 
-export const ErrorStrategies = { placeholder, retry, backoff };
+export const ErrorStrategies = { skip, placeholder, retry, backoff };
 Object.freeze(ErrorStrategies);
\ No newline at end of file
index 3fb2daac4268a051d37cc39fa9319299d9450361..177c46b216b83c3ed16975c2c0e9b979019186da 100644 (file)
@@ -5,7 +5,7 @@ import { csvStreamEqualWritable, createCSVMockStream, assertSleep } from './util
 import { CSVTransformer } from '../src/core.js';
 import { ErrorStrategies } from '../src/errorStrategies.js';
 
-const { placeholder, retry, backoff } = ErrorStrategies;
+const { skip, placeholder, retry, backoff } = ErrorStrategies;
 
 /**
  * Generate a function that will throw errors until it is called **exactly** `count` times.
@@ -38,6 +38,21 @@ function getExponentialSleepBounds(iterations) {
   return { min, max };
 }
 
+suite('ErrorStrategies.skip', { concurrency: true }, () => {
+  test('skips errors', async () => {
+    await createCSVMockStream([
+      ['header', 'header', 'header'],
+      ['1', '1', '1'],
+      ['2', '2', '2'],
+      ['3', '3', '3']
+    ])
+      .pipeThrough(new CSVTransformer(r => { throw new Error(); }, { onError: skip() }))
+      .pipeTo(csvStreamEqualWritable([
+        ['header', 'header', 'header']
+      ]));
+  });
+});
+
 suite('ErrorStrategies.placeholder', { concurrency: true }, () => {
   test('sets placeholders on all fields in a row', async () => {
     await createCSVMockStream([
This page took 0.041032 seconds and 4 git commands to generate.