node-csvtojson icon indicating copy to clipboard operation
node-csvtojson copied to clipboard

Can I access an error that occurs inside subscribe?

Open mikebridge opened this issue 3 years ago • 0 comments

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?

mikebridge avatar Jun 10 '21 20:06 mikebridge