Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js, streams, files and error handling

Questions

  1. How do you handle errors gracefully with streams? My code is becoming really ugly and adding .on('error', () => …) after every .pipe(…) is pain in neck. Is there another way? I read about NodeJS domain but it’s deprecated.

  2. How do you handle errors if they occur somewhere in the middle of the stream? In my case, by that time some of the transformed content has already been transferred back to the user.

My use case is:

  1. User uploads a text file through a typical multipart <form>.
  2. That file is then transformed on the fly using streams.
  3. The result, which is a stream, is piped back to the user and download is triggered.

To put it shortly: You add a file, submit the form, and you get a transformed file back.

Code example

The following code uses express for handling HTTP requests and busboy for handling file upload:

export default Router()
  .get('/', renderPage)
  .post('/', (req, res, next) => {
    const busboy = new Busboy({ headers: req.headers })

    const parser = myParser()

    const transformer = myTransformer()

    busboy.on('file', (_fieldname, file, filename) => {
      res.setHeader('Content-disposition', `attachment; filename=${filename}`)

      file
        .pipe(parser)
        .on('error', err => errorHandler(err, req, res))
        .pipe(transformer)
        .on('error', err => errorHandler(err, req, res))
        .pipe(res)
        .on('error', err => errorHandler(err, req, res))
    })
      .on('error', err => errorHandler(err, req, res))

    req.pipe(busboy)
      .on('error', err => errorHandler(err, req, res))
  })
like image 220
Michał Miszczyszyn Avatar asked Mar 09 '26 05:03

Michał Miszczyszyn


1 Answers

You should check out pump. Seems like it might help with your issues.

like image 169
ruiquelhas Avatar answered Mar 10 '26 19:03

ruiquelhas