Skip to content

Commit

Permalink
fix: more sensible stack trace from dump error
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Dec 6, 2023
1 parent 0c3c6f8 commit a5762f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
40 changes: 17 additions & 23 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,36 +163,30 @@ 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) {
signal?.removeEventListener('abort', onAbort)
if (signal?.aborted) {
reject(signal.reason || Object.assign(new Error('The operation was aborted'), { name: '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, RequestAbortedError)
this.name = 'AbortError'
this.message = message
}
}

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

0 comments on commit a5762f7

Please sign in to comment.