From ac30400a191a78c6148c50e7ff70390a4ea93f90 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 26 Nov 2023 10:25:50 +0100 Subject: [PATCH] fix: dispatch error handling (#2459) --- lib/client.js | 31 ++++++++++++------------------- lib/core/request.js | 34 +++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/lib/client.js b/lib/client.js index cafc03a09ff..22cb39039da 100644 --- a/lib/client.js +++ b/lib/client.js @@ -917,11 +917,9 @@ class Parser { socket[kReset] = true } - let pause - try { - pause = request.onHeaders(statusCode, headers, this.resume, statusText) === false - } catch (err) { - util.destroy(socket, err) + const pause = request.onHeaders(statusCode, headers, this.resume, statusText) === false + + if (request.aborted) { return -1 } @@ -968,13 +966,8 @@ class Parser { this.bytesRead += buf.length - try { - if (request.onData(buf) === false) { - return constants.ERROR.PAUSED - } - } catch (err) { - util.destroy(socket, err) - return -1 + if (request.onData(buf) === false) { + return constants.ERROR.PAUSED } } @@ -1015,11 +1008,7 @@ class Parser { return -1 } - try { - request.onComplete(headers) - } catch (err) { - errorRequest(client, request, err) - } + request.onComplete(headers) client[kQueue][client[kRunningIdx]++] = null @@ -1805,13 +1794,17 @@ function writeH2 (client, session, request) { }) stream.on('data', (chunk) => { - if (request.onData(chunk) === false) stream.pause() + if (request.onData(chunk) === false) { + stream.pause() + } }) stream.once('close', () => { h2State.openStreams -= 1 // TODO(HTTP/2): unref only if current streams count is 0 - if (h2State.openStreams === 0) session.unref() + if (h2State.openStreams === 0) { + session.unref() + } }) stream.once('error', function (err) { diff --git a/lib/core/request.js b/lib/core/request.js index fbbe45a6d9c..3697e6a3acc 100644 --- a/lib/core/request.js +++ b/lib/core/request.js @@ -229,7 +229,11 @@ class Request { onBodySent (chunk) { if (this[kHandler].onBodySent) { - return this[kHandler].onBodySent(chunk) + try { + return this[kHandler].onBodySent(chunk) + } catch (err) { + this.abort(err) + } } } @@ -239,7 +243,11 @@ class Request { } if (this[kHandler].onRequestSent) { - return this[kHandler].onRequestSent() + try { + return this[kHandler].onRequestSent() + } catch (err) { + this.abort(err) + } } } @@ -263,14 +271,23 @@ class Request { channels.headers.publish({ request: this, response: { statusCode, headers, statusText } }) } - return this[kHandler].onHeaders(statusCode, headers, resume, statusText) + try { + return this[kHandler].onHeaders(statusCode, headers, resume, statusText) + } catch (err) { + this.abort(err) + } } onData (chunk) { assert(!this.aborted) assert(!this.completed) - return this[kHandler].onData(chunk) + try { + return this[kHandler].onData(chunk) + } catch (err) { + this.abort(err) + return false + } } onUpgrade (statusCode, headers, socket) { @@ -289,7 +306,13 @@ class Request { if (channels.trailers.hasSubscribers) { channels.trailers.publish({ request: this, trailers }) } - return this[kHandler].onComplete(trailers) + + try { + return this[kHandler].onComplete(trailers) + } catch (err) { + // TODO (fix): This might be a bad idea? + this.onError(err) + } } onError (error) { @@ -303,6 +326,7 @@ class Request { return } this.aborted = true + return this[kHandler].onError(error) }