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

minor hook error handling #3029

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions lib/core/request.js
Expand Up @@ -223,7 +223,11 @@ class Request {
abort(this.error)
} else {
this.abort = abort
return this[kHandler].onConnect(abort)
try {
return this[kHandler].onConnect(abort)
} catch (err) {
abort(err)
}
}
}

Expand Down Expand Up @@ -254,15 +258,18 @@ class Request {
return this[kHandler].onData(chunk)
} catch (err) {
this.abort(err)
return false
}
}

onUpgrade (statusCode, headers, socket) {
assert(!this.aborted)
assert(!this.completed)

return this[kHandler].onUpgrade(statusCode, headers, socket)
try {
return this[kHandler].onUpgrade(statusCode, headers, socket)
} catch (err) {
this.abort(err)
}
}

onComplete (trailers) {
Expand Down Expand Up @@ -295,6 +302,7 @@ class Request {
}
this.aborted = true

// TODO (fix): What if onError throws?
return this[kHandler].onError(error)
}

Expand Down
36 changes: 20 additions & 16 deletions lib/dispatcher/client-h1.js
Expand Up @@ -325,6 +325,7 @@ class Parser {
if (!request) {
return -1
}

request.onResponseStarted()
}

Expand Down Expand Up @@ -408,11 +409,7 @@ class Parser {
client[kQueue][client[kRunningIdx]++] = null
client.emit('disconnect', client[kUrl], [client], new InformationalError('upgrade'))

try {
request.onUpgrade(statusCode, headers, socket)
} catch (err) {
util.destroy(socket, err)
}
request.onUpgrade(statusCode, headers, socket)

client[kResume]()
}
Expand Down Expand Up @@ -556,6 +553,10 @@ class Parser {
if (request.onData(buf) === false) {
return constants.ERROR.PAUSED
}

if (request.aborted) {
return -1
}
}

onMessageComplete () {
Expand Down Expand Up @@ -597,6 +598,10 @@ class Parser {

request.onComplete(headers)

if (request.aborted) {
return -1
}

client[kQueue][client[kRunningIdx]++] = null

if (socket[kWriting]) {
Expand Down Expand Up @@ -915,22 +920,21 @@ function writeH1 (client, request) {

const socket = client[kSocket]

try {
request.onConnect((err) => {
if (request.aborted || request.completed) {
return
}
request.onConnect((err) => {
if (request.aborted || request.completed) {
return
}

errorRequest(client, request, err || new RequestAbortedError())
errorRequest(client, request, err ?? new RequestAbortedError())

if (request.upgrade) {
util.destroy(socket, err ?? new RequestAbortedError('aborted'))
} else {
util.destroy(socket, new InformationalError('aborted'))
})
} catch (err) {
errorRequest(client, request, err)
}
}
})

if (request.aborted) {
util.destroy(body)
return false
}

Expand Down