jtcsv 2.1.5 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,324 @@
1
+ /**
2
+ * Error Handling Examples for jtcsv
3
+ *
4
+ * This file demonstrates proper error handling patterns
5
+ * using jtcsv's typed error classes.
6
+ */
7
+
8
+ const {
9
+ csvToJson,
10
+ jsonToCsv,
11
+ saveAsCsv,
12
+ readCsvAsJson,
13
+ // Error classes
14
+ JtcsvError,
15
+ ValidationError,
16
+ SecurityError,
17
+ ParsingError,
18
+ FileSystemError,
19
+ LimitError,
20
+ ConfigurationError
21
+ } = require('jtcsv');
22
+
23
+ // =============================================================================
24
+ // Example 1: Basic Error Handling
25
+ // =============================================================================
26
+
27
+ function basicErrorHandling() {
28
+ console.log('\n=== Basic Error Handling ===\n');
29
+
30
+ // Invalid input type
31
+ try {
32
+ csvToJson(null);
33
+ } catch (error) {
34
+ if (error instanceof ValidationError) {
35
+ console.log('ValidationError caught:', error.message);
36
+ console.log('Error code:', error.code);
37
+ }
38
+ }
39
+
40
+ // Malformed CSV
41
+ const malformedCsv = 'name,age\n"unclosed quote,25';
42
+ try {
43
+ csvToJson(malformedCsv);
44
+ } catch (error) {
45
+ if (error instanceof ParsingError) {
46
+ console.log('ParsingError caught:', error.message);
47
+ console.log('Line number:', error.lineNumber);
48
+ }
49
+ }
50
+ }
51
+
52
+ // =============================================================================
53
+ // Example 2: Comprehensive Error Handling with Type Checks
54
+ // =============================================================================
55
+
56
+ async function comprehensiveErrorHandling() {
57
+ console.log('\n=== Comprehensive Error Handling ===\n');
58
+
59
+ const operations = [
60
+ {
61
+ name: 'Parse invalid CSV',
62
+ action: () => csvToJson(12345) // Not a string
63
+ },
64
+ {
65
+ name: 'Convert non-array to CSV',
66
+ action: () => jsonToCsv('not an array')
67
+ },
68
+ {
69
+ name: 'Read non-existent file',
70
+ action: async () => await readCsvAsJson('./non-existent-file.csv')
71
+ },
72
+ {
73
+ name: 'Exceed row limit',
74
+ action: () => {
75
+ const csv = 'a,b\n' + '1,2\n'.repeat(100);
76
+ return csvToJson(csv, { maxRows: 5 });
77
+ }
78
+ },
79
+ {
80
+ name: 'Path traversal attempt',
81
+ action: async () => await saveAsCsv([], '../../../etc/passwd.csv')
82
+ }
83
+ ];
84
+
85
+ for (const op of operations) {
86
+ try {
87
+ await op.action();
88
+ console.log(`${op.name}: Success (unexpected)`);
89
+ } catch (error) {
90
+ const errorType = getErrorTypeName(error);
91
+ console.log(`${op.name}: ${errorType} - ${error.message}`);
92
+ }
93
+ }
94
+ }
95
+
96
+ function getErrorTypeName(error) {
97
+ if (error instanceof ValidationError) return 'ValidationError';
98
+ if (error instanceof SecurityError) return 'SecurityError';
99
+ if (error instanceof ParsingError) return 'ParsingError';
100
+ if (error instanceof FileSystemError) return 'FileSystemError';
101
+ if (error instanceof LimitError) return 'LimitError';
102
+ if (error instanceof ConfigurationError) return 'ConfigurationError';
103
+ if (error instanceof JtcsvError) return 'JtcsvError';
104
+ return 'UnknownError';
105
+ }
106
+
107
+ // =============================================================================
108
+ // Example 3: Error Recovery Strategies
109
+ // =============================================================================
110
+
111
+ function errorRecoveryStrategies() {
112
+ console.log('\n=== Error Recovery Strategies ===\n');
113
+
114
+ // Strategy 1: Fallback to default delimiter
115
+ function parseWithFallback(csv) {
116
+ const delimiters = [',', ';', '\t', '|'];
117
+
118
+ for (const delimiter of delimiters) {
119
+ try {
120
+ const result = csvToJson(csv, {
121
+ delimiter,
122
+ autoDetect: false
123
+ });
124
+ console.log(`Successfully parsed with delimiter: "${delimiter}"`);
125
+ return result;
126
+ } catch (error) {
127
+ console.log(`Failed with delimiter "${delimiter}": ${error.message}`);
128
+ }
129
+ }
130
+ throw new Error('Could not parse CSV with any known delimiter');
131
+ }
132
+
133
+ const testCsv = 'name|age|city\nJohn|25|NYC\nJane|30|LA';
134
+ const result = parseWithFallback(testCsv);
135
+ console.log('Result:', result);
136
+ }
137
+
138
+ // =============================================================================
139
+ // Example 4: Batch Processing with Error Collection
140
+ // =============================================================================
141
+
142
+ async function batchProcessingWithErrorCollection() {
143
+ console.log('\n=== Batch Processing with Error Collection ===\n');
144
+
145
+ const csvFiles = [
146
+ { name: 'valid.csv', content: 'a,b\n1,2\n3,4' },
147
+ { name: 'invalid.csv', content: 'a,b\n"unclosed' },
148
+ { name: 'empty.csv', content: '' },
149
+ { name: 'valid2.csv', content: 'x,y\n5,6' }
150
+ ];
151
+
152
+ const results = {
153
+ successful: [],
154
+ failed: []
155
+ };
156
+
157
+ for (const file of csvFiles) {
158
+ try {
159
+ const data = csvToJson(file.content);
160
+ results.successful.push({
161
+ name: file.name,
162
+ rowCount: data.length
163
+ });
164
+ } catch (error) {
165
+ results.failed.push({
166
+ name: file.name,
167
+ error: error.message,
168
+ errorType: getErrorTypeName(error)
169
+ });
170
+ }
171
+ }
172
+
173
+ console.log('Processing Results:');
174
+ console.log('Successful:', results.successful);
175
+ console.log('Failed:', results.failed);
176
+ console.log(`\nSuccess rate: ${results.successful.length}/${csvFiles.length}`);
177
+ }
178
+
179
+ // =============================================================================
180
+ // Example 5: Custom Error Handler Wrapper
181
+ // =============================================================================
182
+
183
+ function createSafeParser(options = {}) {
184
+ const { onError, defaultValue = [] } = options;
185
+
186
+ return function safeParse(csv, parseOptions = {}) {
187
+ try {
188
+ return {
189
+ success: true,
190
+ data: csvToJson(csv, parseOptions),
191
+ error: null
192
+ };
193
+ } catch (error) {
194
+ if (onError) {
195
+ onError(error);
196
+ }
197
+ return {
198
+ success: false,
199
+ data: defaultValue,
200
+ error: {
201
+ type: getErrorTypeName(error),
202
+ message: error.message,
203
+ code: error.code
204
+ }
205
+ };
206
+ }
207
+ };
208
+ }
209
+
210
+ function customErrorHandlerDemo() {
211
+ console.log('\n=== Custom Error Handler ===\n');
212
+
213
+ const safeParse = createSafeParser({
214
+ onError: (error) => {
215
+ console.log(`[Logger] Error occurred: ${error.message}`);
216
+ },
217
+ defaultValue: []
218
+ });
219
+
220
+ // Test with valid CSV
221
+ const result1 = safeParse('a,b\n1,2');
222
+ console.log('Valid CSV result:', result1);
223
+
224
+ // Test with invalid CSV
225
+ const result2 = safeParse(null);
226
+ console.log('Invalid CSV result:', result2);
227
+ }
228
+
229
+ // =============================================================================
230
+ // Example 6: Async Error Handling with Retries
231
+ // =============================================================================
232
+
233
+ async function asyncWithRetries() {
234
+ console.log('\n=== Async Error Handling with Retries ===\n');
235
+
236
+ async function readWithRetry(filePath, maxRetries = 3) {
237
+ let lastError;
238
+
239
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
240
+ try {
241
+ console.log(`Attempt ${attempt}: Reading ${filePath}`);
242
+ const data = await readCsvAsJson(filePath);
243
+ return data;
244
+ } catch (error) {
245
+ lastError = error;
246
+ console.log(`Attempt ${attempt} failed: ${error.message}`);
247
+
248
+ if (error instanceof FileSystemError) {
249
+ // File system errors might be transient
250
+ await new Promise(r => setTimeout(r, 100 * attempt));
251
+ } else if (error instanceof ValidationError) {
252
+ // Validation errors won't resolve with retry
253
+ throw error;
254
+ } else if (error instanceof SecurityError) {
255
+ // Security errors should not be retried
256
+ throw error;
257
+ }
258
+ }
259
+ }
260
+
261
+ throw lastError;
262
+ }
263
+
264
+ try {
265
+ await readWithRetry('./test-data.csv');
266
+ } catch (error) {
267
+ console.log(`All retries failed: ${error.message}`);
268
+ }
269
+ }
270
+
271
+ // =============================================================================
272
+ // Example 7: Schema Validation Error Details
273
+ // =============================================================================
274
+
275
+ function schemaValidationErrors() {
276
+ console.log('\n=== Schema Validation Errors ===\n');
277
+
278
+ const schema = {
279
+ type: 'object',
280
+ required: ['name', 'age'],
281
+ properties: {
282
+ name: { type: 'string', minLength: 1 },
283
+ age: { type: 'number', minimum: 0, maximum: 150 }
284
+ }
285
+ };
286
+
287
+ const data = [
288
+ { name: 'John', age: 25 },
289
+ { name: '', age: 30 }, // Invalid: empty name
290
+ { name: 'Jane', age: -5 }, // Invalid: negative age
291
+ { name: 'Bob', age: 200 } // Invalid: age > 150
292
+ ];
293
+
294
+ try {
295
+ jsonToCsv(data, { schema });
296
+ } catch (error) {
297
+ if (error instanceof ValidationError) {
298
+ console.log('Schema validation failed:');
299
+ console.log('Message:', error.message);
300
+ }
301
+ }
302
+ }
303
+
304
+ // =============================================================================
305
+ // Run All Examples
306
+ // =============================================================================
307
+
308
+ async function main() {
309
+ console.log('jtcsv Error Handling Examples\n');
310
+ console.log('='.repeat(60));
311
+
312
+ basicErrorHandling();
313
+ await comprehensiveErrorHandling();
314
+ errorRecoveryStrategies();
315
+ await batchProcessingWithErrorCollection();
316
+ customErrorHandlerDemo();
317
+ await asyncWithRetries();
318
+ schemaValidationErrors();
319
+
320
+ console.log('\n' + '='.repeat(60));
321
+ console.log('All examples completed.');
322
+ }
323
+
324
+ main().catch(console.error);