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: more sensible stack trace from dump error #2503

Merged
merged 2 commits into from
Dec 6, 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
42 changes: 18 additions & 24 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const assert = require('assert')
const { Readable } = require('stream')
const { RequestAbortedError, NotSupportedError, InvalidArgumentError } = require('../core/errors')
const { RequestAbortedError, NotSupportedError, InvalidArgumentError, AbortError } = require('../core/errors')
const util = require('../core/util')
const { ReadableStreamFrom, toUSVString } = require('../core/util')

Expand Down Expand Up @@ -163,37 +163,31 @@ module.exports = class BodyReadable extends Readable {
return this[kBody]
}

dump (opts) {
let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144
const signal = opts && opts.signal

if (signal) {
try {
if (typeof signal !== 'object' || !('aborted' in signal)) {
throw new InvalidArgumentError('signal must be an AbortSignal')
}
util.throwIfAborted(signal)
} catch (err) {
return Promise.reject(err)
}
async dump (opts) {
let limit = Number.isFinite(opts?.limit) ? opts.limit : 262144
const signal = opts?.signal

if (signal != null && (typeof signal !== 'object' || !('aborted' in signal))) {
throw new InvalidArgumentError('signal must be an AbortSignal')
}

signal?.throwIfAborted()

if (this._readableState.closeEmitted) {
return Promise.resolve(null)
return null
}

return new Promise((resolve, reject) => {
const signalListenerCleanup = signal
? util.addAbortListener(signal, () => {
this.destroy()
})
: noop
return await new Promise((resolve, reject) => {
const onAbort = () => {
this.destroy(signal.reason ?? new AbortError())
}
signal?.addEventListener('abort', onAbort)

this
.on('close', function () {
signalListenerCleanup()
if (signal && signal.aborted) {
reject(signal.reason || Object.assign(new Error('The operation was aborted'), { name: 'AbortError' }))
signal?.removeEventListener('abort', onAbort)
if (signal?.aborted) {
reject(signal.reason ?? new AbortError())
} else {
resolve(null)
}
Expand Down
12 changes: 11 additions & 1 deletion lib/core/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,16 @@ class InvalidReturnValueError extends UndiciError {
}
}

class RequestAbortedError extends UndiciError {
class AbortError extends UndiciError {
constructor (message) {
super(message)
Error.captureStackTrace(this, AbortError)
this.name = 'AbortError'
this.message = message || 'The operation was aborted'
}
}

class RequestAbortedError extends AbortError {
constructor (message) {
super(message)
Error.captureStackTrace(this, RequestAbortedError)
Expand Down Expand Up @@ -207,6 +216,7 @@ class RequestRetryError extends UndiciError {
}

module.exports = {
AbortError,
HTTPParserError,
UndiciError,
HeadersTimeoutError,
Expand Down
Loading