From: Ardis Lu Date: Thu, 6 Mar 2025 06:55:39 +0000 (-0800) Subject: Add skip error strategy X-Git-Tag: v0.1.0~35 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=1109fc49f3828f7efaade3f73d1c83734f713b04;p=csvio Add skip error strategy --- diff --git a/src/errorStrategies.js b/src/errorStrategies.js index b9a24a2..90a73bc 100644 --- a/src/errorStrategies.js +++ b/src/errorStrategies.js @@ -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 diff --git a/test/errorStrategies.test.js b/test/errorStrategies.test.js index 3fb2daa..177c46b 100644 --- a/test/errorStrategies.test.js +++ b/test/errorStrategies.test.js @@ -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([