Skip to content

Commit

Permalink
fix: more sensible stack trace from dump error (#2503)
Browse files Browse the repository at this point in the history
* fix: more sensible stack trace from dump error

* Update lib/core/errors.js

Co-authored-by: Carlos Fuentes <[email protected]>

---------

Co-authored-by: Carlos Fuentes <[email protected]>
  • Loading branch information
ronag and metcoder95 authored Dec 6, 2023
1 parent 0c3c6f8 commit c009d6a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
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

0 comments on commit c009d6a

Please sign in to comment.