nodejs / nodejs/node

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

Aperta
#41,120 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stream
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione fornita e segui lo stack da Stream.ondata fino a internal/streams/writable.js. Eseguila con v14.18.1 e confronta il comportamento osservato con quello di v12.15.0, concentrandoti su come gli errori degli stream collegati tramite pipe raggiungono i gestori degli errori registrati. Il lavoro è completato quando il chunk di array non valido viene segnalato tramite un gestore degli errori senza provocare un arresto anomalo non gestito del processo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, node.js
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.