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: pass ProxyAgent proxy status code error #2162

Merged
merged 2 commits into from
Aug 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ async function httpNetworkFetch (
fetchParams.controller.connection.destroy()

// 2. Return the appropriate network error for fetchParams.
return makeAppropriateNetworkError(fetchParams)
return makeAppropriateNetworkError(fetchParams, err)
}

return makeNetworkError(err)
Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,15 @@ function filterResponse (response, type) {
}

// https://fetch.spec.whatwg.org/#appropriate-network-error
function makeAppropriateNetworkError (fetchParams) {
function makeAppropriateNetworkError (fetchParams, err = null) {
// 1. Assert: fetchParams is canceled.
assert(isCancelled(fetchParams))

// 2. Return an aborted network error if fetchParams is aborted;
// otherwise return a network error.
return isAborted(fetchParams)
? makeNetworkError(new DOMException('The operation was aborted.', 'AbortError'))
: makeNetworkError('Request was cancelled.')
? makeNetworkError(Object.assign(new DOMException('The operation was aborted.', 'AbortError'), { cause: err }))
: makeNetworkError(Object.assign(new DOMException('Request was cancelled.'), { cause: err }))
}

// https://whatpr.org/fetch/1392.html#initialize-a-response
Expand Down
24 changes: 24 additions & 0 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,30 @@ test('should throw when proxy does not return 200', async (t) => {
t.end()
})

test('pass ProxyAgent proxy status code error when using fetch - #2161', async (t) => {
const server = await buildServer()
const proxy = await buildProxy()

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`

proxy.authenticate = function (req, fn) {
fn(null, false)
}

const proxyAgent = new ProxyAgent(proxyUrl)
try {
await fetch(serverUrl, { dispatcher: proxyAgent })
} catch (e) {
t.hasProp(e, 'cause')
}

server.close()
proxy.close()
proxyAgent.close()
t.end()
})

test('Proxy via HTTP to HTTPS endpoint', async (t) => {
t.plan(4)

Expand Down
Loading