Questions
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 NodeJSdomain
but it’s deprecated.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:
- User uploads a text file through a typical multipart
<form>
. - That file is then transformed on the fly using streams.
- 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))
})