Parse Error on socketOnEnd not caught
- Dominant language
- JavaScript
- Stars
- 12.1k
- Forks
- 1.1k
- Avg merge
- 8d 2h
- Merged PRs (30d)
- 21
Description
When the client starts uploading a big file, and close the connection before it is finished, I get the following error logged into the console :
```text
Error: Parse Error
at socketOnEnd (_http_server.js:419:20)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
```
I am using this syntax to handle multer errors, but the callback is never called.
```js
const upload = multer({ dest: appPaths.fromTmpDisk() }).single('firmware')
let isExtracting = false
export function deviceFirmwarePostAPI (ctx, next) {
console.log('isExtracting', isExtracting)
// We prevent users from trying to upload multiple firmware updates at the same time
// The request must be processed one by one
// When one is successful, the device will start updating and then reboot
if (isExtracting) {
ctx.status = SERVER_ERROR.SERVICE_UNAVAILABLE.Value
return
}
return new Promise((resolve) => {
isExtracting = true
console.log('starting upload')
upload(ctx.req, ctx.res, async (err) => {
console.log('err', err) // never called when the clients close the connection before the end of the request
if (err) {
ctx.status = SERVER_ERROR.INTERNAL_SERVER_ERROR.Value
ctx.body = err.message
isExtracting = false
return resolve()
}
try {
const result = await _processXRequest(ctx.req.file) // Ask another process to use the uploaded firmware file
ctx.status = SUCCESS.OK.Value
ctx.body = result
} catch (err) {
// Cleanup uploaded file when the other process cannot handle it
await fs.remove(appPaths.fromTmpDisk(ctx.req.file.filename))
ctx.status = SERVER_ERROR.INTERNAL_SERVER_ERROR.Value
ctx.body = err.message
}
isExtracting = false
return resolve()
})
})
}
```
Versions :
* Node: 8.9.3
* Koa 2.8.1
* multer 1.4.2
Contributor guide
Assessment
This issue has not been assessed yet.