Skip to content

Commit

Permalink
refactor: simplify request error handling (#3364)
Browse files Browse the repository at this point in the history
* refactor: simplify request error handling

* fixup

* fixup
  • Loading branch information
ronag committed Jun 25, 2024
1 parent dd98299 commit 951826c
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,21 @@ class RequestHandler extends AsyncResource {
this.onInfo = onInfo || null
this.throwOnError = throwOnError
this.highWaterMark = highWaterMark
this.signal = signal
this.reason = null
this.removeAbortListener = null

if (util.isStream(body)) {
body.on('error', (err) => {
this.onError(err)
if (signal?.aborted) {
this.reason = signal.reason ?? new RequestAbortedError()
} else if (signal) {
this.removeAbortListener = util.addAbortListener(signal, () => {
this.reason = signal.reason ?? new RequestAbortedError()
if (this.res) {
util.destroy(this.res, this.reason)
} else if (this.abort) {
this.abort(this.reason)
}
})
}

if (this.signal) {
if (this.signal.aborted) {
this.reason = this.signal.reason ?? new RequestAbortedError()
} else {
this.removeAbortListener = util.addAbortListener(this.signal, () => {
this.reason = this.signal.reason ?? new RequestAbortedError()
if (this.res) {
util.destroy(this.res, this.reason)
} else if (this.abort) {
this.abort(this.reason)
}
})
}
}
}

onConnect (abort, context) {
Expand Down Expand Up @@ -174,7 +165,11 @@ class RequestHandler extends AsyncResource {

if (body) {
this.body = null
util.destroy(body, err)

if (util.isStream(body)) {
body.on('error', util.nop)
util.destroy(body, err)
}
}

if (this.removeAbortListener) {
Expand Down

0 comments on commit 951826c

Please sign in to comment.