GeezerVison is showing github.com — lib: use AbortError consistently · nodejs/node@12fb2ff
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/events.js +11-21Lines changed: 11 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -51,15 +51,14 @@ const kRejection = SymbolFor('nodejs.rejection'); | 51 | 51 | let spliceOne; | 52 | 52 |
| 53 | 53 | const { | 54 | | - hideStackFrames, | | 54 | + AbortError, | 55 | 55 | kEnhanceStackBeforeInspector, | 56 | | - codes | | 56 | + codes: { | | 57 | + ERR_INVALID_ARG_TYPE, | | 58 | + ERR_OUT_OF_RANGE, | | 59 | + ERR_UNHANDLED_ERROR | | 60 | + }, | 57 | 61 | } = require('internal/errors'); | 58 | | -const { | 59 | | - ERR_INVALID_ARG_TYPE, | 60 | | - ERR_OUT_OF_RANGE, | 61 | | - ERR_UNHANDLED_ERROR | 62 | | -} = codes; | 63 | 62 |
| 64 | 63 | const { | 65 | 64 | inspect | @@ -76,14 +75,6 @@ const kMaxEventTargetListeners = Symbol('events.maxEventTargetListeners'); | 76 | 75 | const kMaxEventTargetListenersWarned = | 77 | 76 | Symbol('events.maxEventTargetListenersWarned'); | 78 | 77 |
| 79 | | -let DOMException; | 80 | | -const lazyDOMException = hideStackFrames((message, name) => { | 81 | | - if (DOMException === undefined) | 82 | | - DOMException = internalBinding('messaging').DOMException; | 83 | | - return new DOMException(message, name); | 84 | | -}); | 85 | | - | 86 | | - | 87 | 78 | function EventEmitter(opts) { | 88 | 79 | FunctionPrototypeCall(EventEmitter.init, this, opts); | 89 | 80 | } | @@ -713,7 +704,7 @@ async function once(emitter, name, options = {}) { | 713 | 704 | const signal = options?.signal; | 714 | 705 | validateAbortSignal(signal, 'options.signal'); | 715 | 706 | if (signal?.aborted) | 716 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | | 707 | + throw new AbortError(); | 717 | 708 | return new Promise((resolve, reject) => { | 718 | 709 | const errorListener = (err) => { | 719 | 710 | emitter.removeListener(name, resolver); | @@ -738,7 +729,7 @@ async function once(emitter, name, options = {}) { | 738 | 729 | function abortListener() { | 739 | 730 | eventTargetAgnosticRemoveListener(emitter, name, resolver); | 740 | 731 | eventTargetAgnosticRemoveListener(emitter, 'error', errorListener); | 741 | | - reject(lazyDOMException('The operation was aborted', 'AbortError')); | | 732 | + reject(new AbortError()); | 742 | 733 | } | 743 | 734 | if (signal != null) { | 744 | 735 | eventTargetAgnosticAddListener( | @@ -783,9 +774,8 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) { | 783 | 774 | function on(emitter, event, options) { | 784 | 775 | const signal = options?.signal; | 785 | 776 | validateAbortSignal(signal, 'options.signal'); | 786 | | - if (signal?.aborted) { | 787 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 788 | | - } | | 777 | + if (signal?.aborted) | | 778 | + throw new AbortError(); | 789 | 779 |
| 790 | 780 | const unconsumedEvents = []; | 791 | 781 | const unconsumedPromises = []; | @@ -873,7 +863,7 @@ function on(emitter, event, options) { | 873 | 863 | return iterator; | 874 | 864 |
| 875 | 865 | function abortListener() { | 876 | | - errorHandler(lazyDOMException('The operation was aborted', 'AbortError')); | | 866 | + errorHandler(new AbortError()); | 877 | 867 | } | 878 | 868 |
| 879 | 869 | function eventHandler(...args) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +14-16Lines changed: 14 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,7 +80,7 @@ const { | 80 | 80 | ERR_INVALID_ARG_TYPE, | 81 | 81 | ERR_FEATURE_UNAVAILABLE_ON_PLATFORM, | 82 | 82 | }, | 83 | | - hideStackFrames, | | 83 | + AbortError, | 84 | 84 | uvErrmapGet, | 85 | 85 | uvException | 86 | 86 | } = require('internal/errors'); | @@ -148,13 +148,6 @@ let ReadStream; | 148 | 148 | let WriteStream; | 149 | 149 | let rimraf; | 150 | 150 | let rimrafSync; | 151 | | -let DOMException; | 152 | | - | 153 | | -const lazyDOMException = hideStackFrames((message, name) => { | 154 | | - if (DOMException === undefined) | 155 | | - DOMException = internalBinding('messaging').DOMException; | 156 | | - return new DOMException(message, name); | 157 | | -}); | 158 | 151 |
| 159 | 152 | // These have to be separate because of how graceful-fs happens to do it's | 160 | 153 | // monkeypatching. | @@ -324,6 +317,14 @@ function readFileAfterStat(err, stats) { | 324 | 317 | context.read(); | 325 | 318 | } | 326 | 319 |
| | 320 | +function checkAborted(signal, callback) { | | 321 | + if (signal?.aborted) { | | 322 | + callback(new AbortError()); | | 323 | + return true; | | 324 | + } | | 325 | + return false; | | 326 | +} | | 327 | + | 327 | 328 | function readFile(path, options, callback) { | 328 | 329 | callback = maybeCallback(callback || options); | 329 | 330 | options = getOptions(options, { flag: 'r' }); | @@ -342,10 +343,8 @@ function readFile(path, options, callback) { | 342 | 343 | return; | 343 | 344 | } | 344 | 345 |
| 345 | | - if (options.signal?.aborted) { | 346 | | - callback(lazyDOMException('The operation was aborted', 'AbortError')); | | 346 | + if (checkAborted(options.signal, callback)) | 347 | 347 | return; | 348 | | - } | 349 | 348 |
| 350 | 349 | const flagsNumber = stringToFlags(options.flag); | 351 | 350 | path = getValidatedPath(path); | @@ -1459,10 +1458,10 @@ function lutimesSync(path, atime, mtime) { | 1459 | 1458 | function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) { | 1460 | 1459 | if (signal?.aborted) { | 1461 | 1460 | if (isUserFd) { | 1462 | | - callback(lazyDOMException('The operation was aborted', 'AbortError')); | | 1461 | + callback(new AbortError()); | 1463 | 1462 | } else { | 1464 | 1463 | fs.close(fd, function() { | 1465 | | - callback(lazyDOMException('The operation was aborted', 'AbortError')); | | 1464 | + callback(new AbortError()); | 1466 | 1465 | }); | 1467 | 1466 | } | 1468 | 1467 | return; | @@ -1508,10 +1507,9 @@ function writeFile(path, data, options, callback) { | 1508 | 1507 | return; | 1509 | 1508 | } | 1510 | 1509 |
| 1511 | | - if (options.signal?.aborted) { | 1512 | | - callback(lazyDOMException('The operation was aborted', 'AbortError')); | | 1510 | + if (checkAborted(options.signal, callback)) | 1513 | 1511 | return; | 1514 | | - } | | 1512 | + | 1515 | 1513 | fs.open(path, flag, options.mode, (openErr, fd) => { | 1516 | 1514 | if (openErr) { | 1517 | 1515 | callback(openErr); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +20-31Lines changed: 20 additions & 31 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,13 +33,15 @@ const { | 33 | 33 | const binding = internalBinding('fs'); | 34 | 34 | const { Buffer } = require('buffer'); | 35 | 35 |
| 36 | | -const { codes, hideStackFrames } = require('internal/errors'); | 37 | 36 | const { | 38 | | - ERR_FS_FILE_TOO_LARGE, | 39 | | - ERR_INVALID_ARG_TYPE, | 40 | | - ERR_INVALID_ARG_VALUE, | 41 | | - ERR_METHOD_NOT_IMPLEMENTED, | 42 | | -} = codes; | | 37 | + codes: { | | 38 | + ERR_FS_FILE_TOO_LARGE, | | 39 | + ERR_INVALID_ARG_TYPE, | | 40 | + ERR_INVALID_ARG_VALUE, | | 41 | + ERR_METHOD_NOT_IMPLEMENTED, | | 42 | + }, | | 43 | + AbortError, | | 44 | +} = require('internal/errors'); | 43 | 45 | const { isArrayBufferView } = require('internal/util/types'); | 44 | 46 | const { rimrafPromises } = require('internal/fs/rimraf'); | 45 | 47 | const { | @@ -93,13 +95,6 @@ const { | 93 | 95 | const getDirectoryEntriesPromise = promisify(getDirents); | 94 | 96 | const validateRmOptionsPromise = promisify(validateRmOptions); | 95 | 97 |
| 96 | | -let DOMException; | 97 | | -const lazyDOMException = hideStackFrames((message, name) => { | 98 | | - if (DOMException === undefined) | 99 | | - DOMException = internalBinding('messaging').DOMException; | 100 | | - return new DOMException(message, name); | 101 | | -}); | 102 | | - | 103 | 98 | class FileHandle extends EventEmitterMixin(JSTransferable) { | 104 | 99 | constructor(filehandle) { | 105 | 100 | super(); | @@ -272,15 +267,18 @@ async function fsCall(fn, handle, ...args) { | 272 | 267 | } | 273 | 268 | } | 274 | 269 |
| | 270 | +function checkAborted(signal) { | | 271 | + if (signal?.aborted) | | 272 | + throw new AbortError(); | | 273 | +} | | 274 | + | 275 | 275 | async function writeFileHandle(filehandle, data, signal) { | 276 | 276 | // `data` could be any kind of typed array. | 277 | 277 | data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | 278 | 278 | let remaining = data.length; | 279 | 279 | if (remaining === 0) return; | 280 | 280 | do { | 281 | | - if (signal?.aborted) { | 282 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 283 | | - } | | 281 | + checkAborted(signal); | 284 | 282 | const { bytesWritten } = | 285 | 283 | await write(filehandle, data, 0, | 286 | 284 | MathMin(kWriteFileMaxChunkSize, data.length)); | @@ -296,14 +294,11 @@ async function writeFileHandle(filehandle, data, signal) { | 296 | 294 | async function readFileHandle(filehandle, options) { | 297 | 295 | const signal = options?.signal; | 298 | 296 |
| 299 | | - if (signal?.aborted) { | 300 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 301 | | - } | | 297 | + checkAborted(signal); | | 298 | + | 302 | 299 | const statFields = await binding.fstat(filehandle.fd, false, kUsePromises); | 303 | 300 |
| 304 | | - if (signal?.aborted) { | 305 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 306 | | - } | | 301 | + checkAborted(signal); | 307 | 302 |
| 308 | 303 | let size; | 309 | 304 | if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) { | @@ -321,9 +316,7 @@ async function readFileHandle(filehandle, options) { | 321 | 316 | const buffers = []; | 322 | 317 | const fullBuffer = noSize ? undefined : Buffer.allocUnsafeSlow(size); | 323 | 318 | do { | 324 | | - if (signal?.aborted) { | 325 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 326 | | - } | | 319 | + checkAborted(signal); | 327 | 320 | let buffer; | 328 | 321 | let offset; | 329 | 322 | let length; | @@ -693,9 +686,7 @@ async function writeFile(path, data, options) { | 693 | 686 | if (path instanceof FileHandle) | 694 | 687 | return writeFileHandle(path, data, options.signal); | 695 | 688 |
| 696 | | - if (options.signal?.aborted) { | 697 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 698 | | - } | | 689 | + checkAborted(options.signal); | 699 | 690 |
| 700 | 691 | const fd = await open(path, flag, options.mode); | 701 | 692 | const { signal } = options; | @@ -716,9 +707,7 @@ async function readFile(path, options) { | 716 | 707 | if (path instanceof FileHandle) | 717 | 708 | return readFileHandle(path, options); | 718 | 709 |
| 719 | | - if (options.signal?.aborted) { | 720 | | - throw lazyDOMException('The operation was aborted', 'AbortError'); | 721 | | - } | | 710 | + checkAborted(options.signal); | 722 | 711 |
| 723 | 712 | const fd = await open(path, flag, 0o666); | 724 | 713 | return PromisePrototypeFinally(readFileHandle(fd, options), fd.close); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/read_file_context.js +4-12Lines changed: 4 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,15 +10,9 @@ const { Buffer } = require('buffer'); | 10 | 10 |
| 11 | 11 | const { FSReqCallback, close, read } = internalBinding('fs'); | 12 | 12 |
| 13 | | -const { hideStackFrames } = require('internal/errors'); | 14 | | - | 15 | | - | 16 | | -let DOMException; | 17 | | -const lazyDOMException = hideStackFrames((message, name) => { | 18 | | - if (DOMException === undefined) | 19 | | - DOMException = internalBinding('messaging').DOMException; | 20 | | - return new DOMException(message, name); | 21 | | -}); | | 13 | +const { | | 14 | + AbortError, | | 15 | +} = require('internal/errors'); | 22 | 16 |
| 23 | 17 | // Use 64kb in case the file type is not a regular file and thus do not know the | 24 | 18 | // actual file size. Increasing the value further results in more frequent over | @@ -95,9 +89,7 @@ class ReadFileContext { | 95 | 89 | let length; | 96 | 90 |
| 97 | 91 | if (this.signal?.aborted) { | 98 | | - return this.close( | 99 | | - lazyDOMException('The operation was aborted', 'AbortError') | 100 | | - ); | | 92 | + return this.close(new AbortError()); | 101 | 93 | } | 102 | 94 | if (this.size === 0) { | 103 | 95 | buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength); | |
|