Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unhandled exception or failing error body #3137

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions lib/api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ async function getResolveErrorBodyCallback ({ callback, body, contentType, statu
let chunks = []
let length = 0

for await (const chunk of body) {
chunks.push(chunk)
length += chunk.length
if (length > CHUNK_LIMIT) {
chunks = null
break
try {
for await (const chunk of body) {
chunks.push(chunk)
length += chunk.length
if (length > CHUNK_LIMIT) {
chunks = []
length = 0
break
}
}
} catch {
chunks = []
length = 0
// Do nothing....
}

const message = `Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`

if (statusCode === 204 || !contentType || !chunks) {
if (statusCode === 204 || !contentType || !length) {
queueMicrotask(() => callback(new ResponseStatusCodeError(message, statusCode, headers)))
return
}
Expand Down
21 changes: 21 additions & 0 deletions test/issue-3136.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { request } = require('..')
const { test, after } = require('node:test')
const net = require('node:net')
const { once } = require('node:events')
const assert = require('node:assert')

test('https://github.com/mcollina/undici/issues/3136', async (t) => {
const server = net.createServer((socket) => {
socket.write('HTTP/1.1 404 Not Found\r\n')
socket.write('Transfer-Encoding: chunked\r\n\r\n')
socket.write('\r\n')
})
after(() => server.close())
server.listen(0)
await once(server, 'listening')
await assert.rejects(
request(`http://localhost:${server.address().port}`, {
throwOnError: true
})
)
})