You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using multiparty to read and parse files having a specific data layout. In the event handler of form.on('part', function(...) { }), I am calling my own internal method readFile() to read and parse the file data. It works well for success scenario, where the file content is correct. I am having problems in aborting the read/parse operation, when I encounter parse error. Basically I do not know, how to propagate the error to the middleware handler so, it can abort and send http error response back to caller.
My setup of using multiparty is as below:
/** * Middleware to read n parse uploaded multi-part form data * for campaign creation. * After successful parse, sends to next layer. **/varmp=require('multiparty');module.exports=function(req,res,next){console.log("File processor at work...");varform=newmp.Form();form.on('part',function(part){if(!part.filename){part.resume();}else{part.setEncoding('utf8');part.on('readable',function(){readFile(req,res,part);});part.on('error',function(e){console.log("Am i reached??");//return res.status(400).json(e);form.emit('error',e);})}});form.on('error',function(err){console.log("Error while processing upload.",3);res.status(400).json(err);});form.on('close',function(){console.log("Reading finished. Forwarding to next layer...");returnnext();});form.parse(req);}
In the above snippet, 'readFile' is the function in which I read from 'part' which is a readable stream. I wrote an FSM based parser, that reads each char and accumulates relevant data structures. Whenever I find something incorrect in the received char, I want to abort the whole processing. I do not know how to stop this processing. Here is my code for readFile.
/** * Reads the file data from a reader stream **/functionreadFile(req,res,reader){console.log("Reading data...");
...
while((ch=reader.read(1))!==null){req.state=req.state||READING_HDR_KEY;switch(req.state){caseREADING_HDR_KEY:
...
break;caseREADING_HDR_VAL:
...
break;caseREADING_LINE_BREAK:
...
break;caseREADING_MSISDN:
...
elseif(isalphabet(ch)){//throw new Error('Parser error: MSISDN cannot be alphanumeric.'); //process.nextTick(function() { //reader.emit('error', 'Parser error. eeek');// ??? What to do here???return;//})//return res.status(400).json('Parser error: MSISDN cannot be alphanumeric.');}else{msisdn+=ch;}break;caseREADING_COMMA_OR_SPACE:
...
break;default:
thrownewError("Parser Error: Unknown state encountered");}//EOF:switch}//EOF:while//});}
I tried a few things such as:
Throwing an exception from readFile(). It got caught by outer fn i.e. form.on('part'...), but was not helpful in aborting.
Tried emitting an error event from readFile(), but same behaviour.
So, how do I decently stop processing on parse error?
The text was updated successfully, but these errors were encountered:
Hi,
I am using multiparty to read and parse files having a specific data layout. In the event handler of form.on('part', function(...) { }), I am calling my own internal method readFile() to read and parse the file data. It works well for success scenario, where the file content is correct. I am having problems in aborting the read/parse operation, when I encounter parse error. Basically I do not know, how to propagate the error to the middleware handler so, it can abort and send http error response back to caller.
My setup of using multiparty is as below:
In the above snippet, 'readFile' is the function in which I read from 'part' which is a readable stream. I wrote an FSM based parser, that reads each char and accumulates relevant data structures. Whenever I find something incorrect in the received char, I want to abort the whole processing. I do not know how to stop this processing. Here is my code for readFile.
I tried a few things such as:
So, how do I decently stop processing on parse error?
The text was updated successfully, but these errors were encountered: