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(api-stream): Add throwOnError support in undici.stream #1767

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { finished } = require('stream')
const {
InvalidArgumentError,
InvalidReturnValueError,
RequestAbortedError
RequestAbortedError, ResponseStatusCodeError
} = require('../core/errors')
const util = require('../core/util')
const { AsyncResource } = require('async_hooks')
Expand All @@ -16,7 +16,7 @@ class StreamHandler extends AsyncResource {
throw new InvalidArgumentError('invalid opts')
}

const { signal, method, opaque, body, onInfo, responseHeaders } = opts
const { signal, method, opaque, body, onInfo, responseHeaders, throwOnError } = opts

try {
if (typeof callback !== 'function') {
Expand Down Expand Up @@ -57,6 +57,7 @@ class StreamHandler extends AsyncResource {
this.trailers = null
this.body = body
this.onInfo = onInfo || null
this.throwOnError = throwOnError

if (util.isStream(body)) {
body.on('error', (err) => {
Expand All @@ -76,7 +77,7 @@ class StreamHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, rawHeaders, resume) {
onHeaders (statusCode, rawHeaders, resume, statusMessage) {
const { factory, opaque, context } = this

if (statusCode < 200) {
Expand Down Expand Up @@ -116,6 +117,12 @@ class StreamHandler extends AsyncResource {
}

this.callback = null

if (this.throwOnError && statusCode >= 400) {
this.runInAsyncScope(callback, null, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
return
}

this.runInAsyncScope(callback, null, err || null, { opaque, trailers })

if (err) {
Expand Down
6 changes: 3 additions & 3 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ function isDisturbed (body) {
stream.isDisturbed
? stream.isDisturbed(body) || body[kBodyUsed] // TODO (fix): Why is body[kBodyUsed] needed?
: body[kBodyUsed] ||
body.readableDidRead ||
(body._readableState && body._readableState.dataEmitted) ||
isReadableAborted(body)
body.readableDidRead ||
(body._readableState && body._readableState.dataEmitted) ||
isReadableAborted(body)
))
}

Expand Down
39 changes: 39 additions & 0 deletions test/client-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,42 @@ test('stream legacy needDrain', (t) => {
})
})
})

test('steam throw if statusCode >= 400', (t) => {
t.plan(1)

const expectedBodyContent = 'expected_body_content'

const server = createServer((req, res) => {
res.writeHead(400, { 'Content-Type': 'text/plain' })
res.end(expectedBodyContent)
})
t.teardown(server.close.bind(server))

server.listen(0, async () => {
const client = new Client(`http://localhost:${server.address().port}`)
t.teardown(client.close.bind(client))

const passThrough = new PassThrough()
const chunks = []

passThrough.on('data', (chunk) => {
chunks.push(chunk)
})

try {
await client.stream({
path: '/',
method: 'GET',
throwOnError: true,
opaque: passThrough
}, ({ opaque }) => opaque)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not expect this factory to be called at all.


t.fail('No Error')
} catch (e) {
const actualBodyContent = Buffer.concat(chunks).toString()

t.equal(actualBodyContent, expectedBodyContent)
}
})
})