node-csvtojson
node-csvtojson copied to clipboard
Can I access an error that occurs inside subscribe?
I have this:
const result = await csv()
.fromFile(filePath)
.subscribe(async (json, lineNumber) => {
const saveResult = await saveToDb(json);
if (!saveResult.isSuccessful) {
log.error('throwing exception');
log.error(saveResult.exception); // this shows the correct error message
throw new Error(saveResult.exception);
}
return saveResult;
}, (error) => {
log.error('subscribe.error returning error');
log.error(error); // this seems to be undefined??
return error;
}, () => {
log.info('subscribe.completed');
});
I expected the error handler to receive the thrown error saveResult.exception
, but error
is undefined.
If I throw the Error, it seems to cause the promise to terminate incorrectly. If I don't throw the error, the result
is the json from the parsing, not the result of the subscription processing.
Is there a way to get the error message out of subscribe
?