3

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))
  })
Michał Miszczyszyn
  • 11,835
  • 2
  • 35
  • 53
  • 1
    Can't you have just one `.on('error', ...)` at the end of your pipe chain? – jfriend00 Aug 06 '18 at 19:29
  • What to do when an errors occurs in the middle of a data transfer are always application specific and often require appropriate error handling on the other end too. – jfriend00 Aug 06 '18 at 19:30
  • @jfriend00 I understand that. I was wondering what's the common/best way to tell the client that an error occured at this point. Headers are already sent, it's too late for `5xx` status code. – Michał Miszczyszyn Aug 06 '18 at 19:31
  • If that's your question, then please edit your question to state it more succinctly as that. That's not what I got from reading your question. – jfriend00 Aug 06 '18 at 19:32
  • 1
    Perhaps this: [What to do with errors when streaming the body of an Http request](https://stackoverflow.com/a/15305631/816620). – jfriend00 Aug 06 '18 at 19:35

1 Answers1

1

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

ruiquelhas
  • 1,905
  • 1
  • 17
  • 17