nodejs / nodejs/node

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

Open
#41,120 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stream
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided reproduction and trace the stack from Stream.ondata into internal/streams/writable.js. Run it against v14.18.1 and compare the reported behavior with v12.15.0, focusing on how errors from the piped streams reach the registered error handlers. Done means the invalid array chunk is reported through an error handler without an uncaught process crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.