Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handler for io functions #503

Merged
merged 4 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/danfojs-base/io/browser/io.csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const $streamCSV = async (file: string, callback: (df: DataFrame) => void, optio
const frameConfig = options?.frameConfig || {}

return new Promise(resolve => {
let count = -1
let count = 0
Papa.parse(file, {
...options,
dynamicTyping: true,
Expand Down Expand Up @@ -174,4 +174,4 @@ export {
$readCSV,
$streamCSV,
$toCSV,
}
}
2 changes: 1 addition & 1 deletion src/danfojs-base/io/browser/io.excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
export {
$readExcel,
$toExcel
}
}
204 changes: 127 additions & 77 deletions src/danfojs-base/io/node/io.csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const $readCSV = async (filePath: string, options?: CsvInputOptionsNode): Promis
const frameConfig = options?.frameConfig || {}

if (filePath.startsWith("http") || filePath.startsWith("https")) {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
const optionsWithDefaults = {
header: true,
dynamicTyping: true,
Expand All @@ -60,6 +60,13 @@ const $readCSV = async (filePath: string, options?: CsvInputOptionsNode): Promis
}

const dataStream = request.get(filePath);
// reject any non-2xx status codes
dataStream.on('response', (response: any) => {
if (response.statusCode < 200 || response.statusCode >= 300) {
reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
}
});

const parseStream: any = Papa.parse(Papa.NODE_STREAM_INPUT, optionsWithDefaults as any);
dataStream.pipe(parseStream);

Expand All @@ -74,17 +81,24 @@ const $readCSV = async (filePath: string, options?: CsvInputOptionsNode): Promis
});

} else {
return new Promise(resolve => {
const fileStream = fs.createReadStream(filePath)
Papa.parse(fileStream, {
header: true,
dynamicTyping: true,
...options,
complete: results => {
const df = new DataFrame(results.data, frameConfig);
resolve(df);
return new Promise((resolve, reject) => {
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
reject("ENOENT: no such file or directory");
}
});

const fileStream = fs.createReadStream(filePath)

Papa.parse(fileStream, {
header: true,
dynamicTyping: true,
...options,
complete: results => {
const df = new DataFrame(results.data, frameConfig);
resolve(df);
}
});
})
});
}
};
Expand Down Expand Up @@ -113,9 +127,17 @@ const $streamCSV = async (filePath: string, callback: (df: DataFrame) => void, o
dynamicTyping: true,
...options,
}
return new Promise(resolve => {
let count = -1
return new Promise((resolve, reject) => {
let count = 0
const dataStream = request.get(filePath);

// reject any non-2xx status codes
dataStream.on('response', (response: any) => {
if (response.statusCode < 200 || response.statusCode >= 300) {
reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
}
});

const parseStream: any = Papa.parse(Papa.NODE_STREAM_INPUT, optionsWithDefaults);
dataStream.pipe(parseStream);

Expand All @@ -130,19 +152,26 @@ const $streamCSV = async (filePath: string, callback: (df: DataFrame) => void, o

});
} else {
const fileStream = fs.createReadStream(filePath)

return new Promise(resolve => {
let count = -1
Papa.parse(fileStream, {
header: true,
dynamicTyping: true,
...options,
step: results => {
const df = new DataFrame([results.data], { ...frameConfig, index: [count++] });
callback(df);
},
complete: () => resolve(null)
return new Promise((resolve, reject) => {
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
reject("ENOENT: no such file or directory");
}

const fileStream = fs.createReadStream(filePath)

let count = 0
Papa.parse(fileStream, {
header: true,
dynamicTyping: true,
...options,
step: results => {
const df = new DataFrame([results.data], { ...frameConfig, index: [count++] });
callback(df);
},
complete: () => resolve(null)
});
});
});
}
Expand Down Expand Up @@ -228,9 +257,17 @@ const $openCsvInputStream = (filePath: string, options: CsvInputOptionsNode) =>

if (filePath.startsWith("http") || filePath.startsWith("https")) {
const dataStream = request.get(filePath);

// reject any non-2xx status codes
dataStream.on('response', (response: any) => {
if (response.statusCode < 200 || response.statusCode >= 300) {
throw new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`);
}
});

const parseStream: any = Papa.parse(Papa.NODE_STREAM_INPUT, { header, dynamicTyping: true, ...options });
dataStream.pipe(parseStream);
let count = -1
let count = 0

parseStream.on("data", (chunk: any) => {
if (isFirstChunk) {
Expand Down Expand Up @@ -258,37 +295,44 @@ const $openCsvInputStream = (filePath: string, options: CsvInputOptionsNode) =>
return csvInputStream;
} else {
const fileStream = fs.createReadStream(filePath)
let count = -1
Papa.parse(fileStream, {
...{ header, dynamicTyping: true, ...options },
step: results => {
if (isFirstChunk) {
if (header === true) {
ndFrameColumnNames = results.meta.fields || []
} else {
ndFrameColumnNames = results.data

fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
throw new Error("ENOENT: no such file or directory");
}

let count = 0
Papa.parse(fileStream, {
...{ header, dynamicTyping: true, ...options },
step: results => {
if (isFirstChunk) {
if (header === true) {
ndFrameColumnNames = results.meta.fields || []
} else {
ndFrameColumnNames = results.data
}
isFirstChunk = false
return
}
isFirstChunk = false
return

const df = new DataFrame([results.data], {
columns: ndFrameColumnNames,
index: [count++]
})

csvInputStream.push(df);
},
complete: (result: any) => {
csvInputStream.push(null);
return null
},
error: (err) => {
csvInputStream.emit("error", err);
}
});

const df = new DataFrame([results.data], {
columns: ndFrameColumnNames,
index: [count++]
})

csvInputStream.push(df);
},
complete: (result: any) => {
csvInputStream.push(null);
return null
},
error: (err) => {
csvInputStream.emit("error", err);
}
return csvInputStream;
});

return csvInputStream;
}
};

Expand All @@ -314,39 +358,45 @@ const $openCsvInputStream = (filePath: string, options: CsvInputOptionsNode) =>
* ```
*/
const $writeCsvOutputStream = (filePath: string, options: CsvInputOptionsNode) => {
let isFirstRow = true
const fileOutputStream = fs.createWriteStream(filePath)
const csvOutputStream = new stream.Writable({ objectMode: true })
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
throw new Error("ENOENT: no such file or directory");
}

let isFirstRow = true
const fileOutputStream = fs.createWriteStream(filePath)
const csvOutputStream = new stream.Writable({ objectMode: true })

csvOutputStream._write = (chunk: DataFrame | Series, encoding, callback) => {
csvOutputStream._write = (chunk: DataFrame | Series, encoding, callback) => {

if (chunk instanceof DataFrame) {
if (chunk instanceof DataFrame) {

if (isFirstRow) {
isFirstRow = false
fileOutputStream.write($toCSV(chunk, { header: true, ...options }));
if (isFirstRow) {
isFirstRow = false
fileOutputStream.write($toCSV(chunk, { header: true, ...options }));
callback();
} else {
fileOutputStream.write($toCSV(chunk, { header: false, ...options }));
callback();
}

} else if (chunk instanceof Series) {

fileOutputStream.write($toCSV(chunk));
callback();

} else {
fileOutputStream.write($toCSV(chunk, { header: false, ...options }));
callback();
csvOutputStream.emit("error", new Error("ValueError: Intermediate chunk must be either a Series or DataFrame"))
}

} else if (chunk instanceof Series) {

fileOutputStream.write($toCSV(chunk));
callback();

} else {
csvOutputStream.emit("error", new Error("ValueError: Intermediate chunk must be either a Series or DataFrame"))
}

}

csvOutputStream.on("finish", () => {
fileOutputStream.end()
})
csvOutputStream.on("finish", () => {
fileOutputStream.end()
})

return csvOutputStream
return csvOutputStream
});
}


Expand All @@ -358,4 +408,4 @@ export {
$toCSV,
$writeCsvOutputStream,
$openCsvInputStream,
}
}
26 changes: 17 additions & 9 deletions src/danfojs-base/io/node/io.excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
readFile,
utils
} from "xlsx";
import fs from 'fs'

/**
* Reads a JSON file from local or remote location into a DataFrame.
* @param filePath URL or local file path to JSON file.
Expand Down Expand Up @@ -54,7 +56,7 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})

if (filePath.startsWith("http") || filePath.startsWith("https")) {

return new Promise(resolve => {
return new Promise((resolve, reject) => {
fetch(filePath, { method, headers }).then(response => {
if (response.status !== 200) {
throw new Error(`Failed to load ${filePath}`)
Expand All @@ -68,17 +70,23 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})
resolve(df);
});
}).catch((err) => {
throw new Error(err)
reject(err)
})
})

} else {
return new Promise(resolve => {
const workbook = readFile(filePath);
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
const data = utils.sheet_to_json(worksheet);
const df = new DataFrame(data, frameConfig);
resolve(df);
return new Promise((resolve, reject) => {
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
reject("ENOENT: no such file or directory");
}

const workbook = readFile(filePath);
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
const data = utils.sheet_to_json(worksheet);
const df = new DataFrame(data, frameConfig);
resolve(df);
})
});
}
};
Expand Down Expand Up @@ -126,4 +134,4 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
export {
$readExcel,
$toExcel
}
}
Loading