nodejs / nodejs/node

Unable to catch error in piped streams | Uncaught TypeError [ERR_INVALID_ARG_TYPE]

未关闭
#41,120 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stream
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v14.18.1

Platform

macOS Big Sur

Subsystem

No response

What steps will reproduce the bug?

When we pipe multiple streams and the data coming from the first stream is an array, then it is expected to have an error. But I am unable to catch this error in this case and it is throwing the below error.

**internal/streams/writable.js:285
throw new ERR_INVALID_ARG_TYPE(
^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array
at new NodeError (internal/errors.js:322:7)
at WriteStream.Writable.write (internal/streams/writable.js:285:13)
at Stream.ondata (internal/streams/legacy.js:20:31)
at Stream.emit (events.js:400:28)
at drain (/Users/mayankkatare/Trainings/Node assignments/node_modules/through/index.js:36:16)
at Stream.stream.queue.stream.push (/Users/mayankkatare/Trainings/Node assignments/node_modules/through/index.js:45:5)
at Parser.parser.onValue (/Users/mayankkatare/Trainings/Node assignments/node_modules/JSONStream/index.js:118:16)
at Parser.proto.emit (/Users/mayankkatare/Trainings/Node assignments/node_modules/jsonparse/jsonparse.js:337:8)
at Parser.proto.pop (/Users/mayankkatare/Trainings/Node assignments/node_modules/jsonparse/jsonparse.js:332:8)
at Parser.proto.onToken (/Users/mayankkatare/Trainings/Node assignments/node_modules/jsonparse/jsonparse.js:402:12) {
code: 'ERR_INVALID_ARG_TYPE'
}**

------Repro script--------

const request = require('request');
const JSONStream = require('JSONStream')
const fs = require('fs')

let Url = 'http://localhost:5000/' // This url returns a response 

request(Url).on('response', respHandler)

function respHandler(res) {
  let jsonStream = JSONStream.parse(['data']) // Assuming response is {data:['Home page', 'Jackson']}
  
  var file = fs.createWriteStream('someNewFile.txt')
  try {
// Trying to catch error on each stream initially
    res.on('error', err => console.log(err));
    jsonStream.on('error', err => console.log(err));
    file.on('error', err => console.log(err));
    
// Trying to catch error during piping
    res.pipe(jsonStream).on('error', (err) => {
      console.log(err)
      if(err) {
        console.log(err);
      }
    }).pipe(file).on('error', (err) => {
      console.log(err)
      if(err) {
        console.log(err);
      }
    })
  } catch (error) {
    if(error)
    console.log(error);
  }
  
}
How often does it reproduce? Is there a required condition?

Everytime

What is the expected behavior?

It should throw an error but we should be able to handle this error so that our app should not crash. We should be able to catch the error in error event handler. This was the behaviour in 12.15.0 and it seems to have changed in 14.18.1

What do you see instead?

Uncaught TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array

Additional information

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从提供的复现开始,跟踪从 Stream.ondata 到 internal/streams/writable.js 的调用栈。在 v14.18.1 上运行它,并将报告的行为与 v12.15.0 进行比较,重点关注来自管道流的错误如何到达已注册的错误处理器。完成的标准是:无效的数组块通过错误处理器报告,且不会导致进程发生未捕获崩溃。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。