在Node中使用Multiparty时,如何正确抛出错误?

时间:2017-06-18 21:51:49

标签: javascript node.js express file-upload multipart

我尝试调用错误处理程序函数并返回响应而不会导致进程崩溃。代码很简单:

// Parts are emitted when parsing the form
form.on('part', function(part) {
  // You *must* act on the part by reading it
  // NOTE: if you want to ignore it, just call "part.resume()"

  if (!part.filename) {
    // filename is not defined when this is a field and not a file
    console.log('got field named ' + part.name);
    // ignore field's content
    part.resume();
  }

  if (part.filename) {
    // filename is defined when this is a file
    console.log('got file named ' + part.filename);
    if (part.filename == "CHFILE.TXT"){
      records = CHfilter(part);
    } else if (part.filename == "FCFILE.TXT"){
      records = FCfilter(part);
    } else if (part.filename == "TRFILE.TXT"){
      records = TRfilter(part);
    } else if (part.filename == "TLFILE.TXT"){
      records = TLfilter(part);
    } else {
      throw new Error("File "+part.filename+" is not a recognized file.")
    }

    // ignore file's content here
    part.resume();
  }

  part.on('error', function(err) {
    // decide what to do
    if (err) Log.resError(err, req, res);
    console.log("error function");
    res.send(err);
  });
});

但是当我上传任何其他名称的文件时,它会抛出错误,但part.on('error',...函数永远不会运行。所以客户永远不会得到回复。我错过了什么?

谢谢!

0 个答案:

没有答案