nodejs / nodejs/node

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

Đang mở
#41,120 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stream
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với bản tái hiện được cung cấp và lần theo stack từ Stream.ondata đến internal/streams/writable.js. Chạy nó trên v14.18.1 và so sánh hành vi được báo cáo với v12.15.0, tập trung vào cách các lỗi từ những stream được pipe truyền đến các error handler đã đăng ký. Hoàn tất khi chunk array không hợp lệ được báo cáo thông qua một error handler mà không gây ra process crash không được bắt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, node.js
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.