Christopher Mellor
Christopher Mellor

Reputation: 444

Error: Data should not be empty or the "fields" option should be included

I've got some code that looks like this

try {
return inputStream
  .pipe(JSONStream.stringify())
  .on('error', e => next(`Stringify error: ${e}`))
  .pipe(Json2csvTransform)
  .on('error', e => next(`json2csv error: ${e}`))
  .pipe(res)
  .on('finish', () => console.log("Streaming of file is now complete."))
 } catch(error) {
return res.status(400).json({ message: msg('fileRetrievalError', -3) })
}

and when I reach .on('error', e => next('json2csv error: ${e}'))

the process does not fall through to the catch, it just keeps on keeping on. I'm guessing this is because it's wrapped in a next

The error i finally was able to extract is this:

Error: Data should not be empty or the "fields" option should be included

I tried to dig through the source in the node modules but it didn't mean much to me.

I guess i have two possible solutions: either I need to understand what the error coming from Json2csv means, or i need to be able to exit and close my stream. i tried to just shove return res.status(400).json({ message: msg('fileRetrievalError', -3) }) into the callback of the .on('error;), but if the process fails one time, it fails every time until the session ends, even when giving it all valid information - if that makes sense..

I don't know a ton about node, and this package doesn't have a lot of support surrounding it.

Upvotes: 1

Views: 5752

Answers (1)

Randy Casburn
Randy Casburn

Reputation: 14175

If you want to stop processing the stream after processing a particular error, you must throw the error:

.on('error', e => throw error.message)

Upvotes: 1

Related Questions