Skip to content

Commit

Permalink
fix: unhandled exception or failing error body
Browse files Browse the repository at this point in the history
Refs: #3136
  • Loading branch information
ronag committed Apr 19, 2024
1 parent b482512 commit a13a9f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
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
9 changes: 9 additions & 0 deletions test/issue-3136.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { request } = require('..')

request('http://www.rolbox.it/rk-kastelruth/webcam/web03.jpg', { throwOnError: true })
.then(({ statusCode, body }) => {
console.log('then called', { statusCode })
body.on('err', e => console.error('body error:', e))
})
.catch(e => console.error('catch called:', e))
.finally(() => console.log('finally called'))

0 comments on commit a13a9f5

Please sign in to comment.