feat: adding try catch code
This commit is contained in:
7
index.js
7
index.js
@ -1,7 +1,12 @@
|
|||||||
const { readFile } = require('./utils/converter')
|
const { readFile } = require('./utils/converter')
|
||||||
|
|
||||||
const add = async (path) => {
|
const add = async (path) => {
|
||||||
return await readFile(`${path}`)
|
try {
|
||||||
|
if (typeof path !== 'string') throw new Error('path have to be an string')
|
||||||
|
return await readFile(`${path}`)
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e.message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = add
|
module.exports = add
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "async-convert-csv-to-json",
|
"name": "async-convert-csv-to-json",
|
||||||
"version": "1.0.1",
|
"version": "1.1.1",
|
||||||
"description": "convert csv file to json",
|
"description": "convert csv file to json",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
11
utils/MOCK_DATA.csv
Normal file
11
utils/MOCK_DATA.csv
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
id,first_name,last_name,email,gender,ip_address
|
||||||
|
1,Rowney,Vanlint,rvanlint0@archive.org,Male,166.243.73.22
|
||||||
|
2,Granny,Zecchetti,gzecchetti1@toplist.cz,Bigender,76.2.20.212
|
||||||
|
3,Elena,Quilkin,equilkin2@artisteer.com,Female,206.153.61.10
|
||||||
|
4,Rob,Kilkenny,rkilkenny3@spiegel.de,Genderqueer,56.156.35.222
|
||||||
|
5,Alayne,Sleit,asleit4@si.edu,Female,105.12.142.0
|
||||||
|
6,Arleta,Radsdale,aradsdale5@psu.edu,Female,20.128.1.2
|
||||||
|
7,Maximilian,Oswick,moswick6@hc360.com,Male,241.240.199.79
|
||||||
|
8,Gil,Chinnick,gchinnick7@slashdot.org,Male,2.145.45.118
|
||||||
|
9,Galen,Goulter,ggoulter8@sakura.ne.jp,Male,37.93.133.252
|
||||||
|
10,Samara,Benka,sbenka9@nydailynews.com,Female,225.2.115.39
|
|
@ -1,4 +1,5 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
const csvToJson = (csv) => {
|
const csvToJson = (csv) => {
|
||||||
const lines = csv.split(/\r\n|\n/)
|
const lines = csv.split(/\r\n|\n/)
|
||||||
@ -17,6 +18,8 @@ const csvToJson = (csv) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.readFile = async (pathFile) => {
|
exports.readFile = async (pathFile) => {
|
||||||
|
const checkExt = path.extname(pathFile)
|
||||||
|
if (checkExt !== '.csv') throw new Error('Wrong extension file')
|
||||||
const result = new Promise((resolve, reject) => {
|
const result = new Promise((resolve, reject) => {
|
||||||
fs.readFile(`${pathFile}`, 'utf8', (err, data) => {
|
fs.readFile(`${pathFile}`, 'utf8', (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
Reference in New Issue
Block a user