convert-csv-to-json 3.27.0 → 4.0.0

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.
@@ -1,23 +1,31 @@
1
1
  'use strict';
2
2
 
3
- let fs = require('fs');
3
+ const fs = require('fs');
4
+ const { FileOperationError } = require('./errors');
4
5
 
5
6
  class FileUtils {
6
7
 
7
8
  readFile(fileInputName, encoding) {
8
- return fs.readFileSync(fileInputName, encoding).toString();
9
+ try {
10
+ return fs.readFileSync(fileInputName, encoding).toString();
11
+ } catch (error) {
12
+ throw new FileOperationError('read', fileInputName, error);
13
+ }
9
14
  }
10
15
 
11
16
  readFileAsync(fileInputName, encoding = 'utf8') {
12
17
  // Use fs.promises when available for a Promise-based API
13
18
  if (fs.promises && typeof fs.promises.readFile === 'function') {
14
19
  return fs.promises.readFile(fileInputName, encoding)
15
- .then(buf => buf.toString());
20
+ .then(buf => buf.toString())
21
+ .catch(error => {
22
+ throw new FileOperationError('read', fileInputName, error);
23
+ });
16
24
  }
17
25
  return new Promise((resolve, reject) => {
18
26
  fs.readFile(fileInputName, encoding, (err, data) => {
19
27
  if (err) {
20
- reject(err);
28
+ reject(new FileOperationError('read', fileInputName, err));
21
29
  return;
22
30
  }
23
31
  resolve(data.toString());
@@ -28,7 +36,7 @@ class FileUtils {
28
36
  writeFile(json, fileOutputName) {
29
37
  fs.writeFile(fileOutputName, json, function (err) {
30
38
  if (err) {
31
- throw err;
39
+ throw new FileOperationError('write', fileOutputName, err);
32
40
  } else {
33
41
  console.log('File saved: ' + fileOutputName);
34
42
  }
@@ -37,11 +45,14 @@ class FileUtils {
37
45
 
38
46
  writeFileAsync(json, fileOutputName) {
39
47
  if (fs.promises && typeof fs.promises.writeFile === 'function') {
40
- return fs.promises.writeFile(fileOutputName, json);
48
+ return fs.promises.writeFile(fileOutputName, json)
49
+ .catch(error => {
50
+ throw new FileOperationError('write', fileOutputName, error);
51
+ });
41
52
  }
42
53
  return new Promise((resolve, reject) => {
43
54
  fs.writeFile(fileOutputName, json, (err) => {
44
- if (err) return reject(err);
55
+ if (err) return reject(new FileOperationError('write', fileOutputName, err));
45
56
  resolve();
46
57
  });
47
58
  });
@@ -1,12 +1,14 @@
1
1
  'use strict';
2
2
 
3
+ const { JsonValidationError } = require('./errors');
4
+
3
5
  class JsonUtil {
4
6
 
5
7
  validateJson(json) {
6
8
  try {
7
9
  JSON.parse(json);
8
10
  } catch (err) {
9
- throw Error('Parsed csv has generated an invalid json!!!\n' + err);
11
+ throw new JsonValidationError(json, err);
10
12
  }
11
13
  }
12
14
 
File without changes