Skip to content

Commit 12fb2ff

Browse files
jasnelldanielleadams
authored andcommittedMar 16, 2021
lib: use AbortError consistently
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #37715 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent c737df6 commit 12fb2ff

File tree

4 files changed

+49
-80
lines changed

4 files changed

+49
-80
lines changed
 

‎lib/events.js

+11-21
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ const kRejection = SymbolFor('nodejs.rejection');
5151
let spliceOne;
5252

5353
const {
54-
hideStackFrames,
54+
AbortError,
5555
kEnhanceStackBeforeInspector,
56-
codes
56+
codes: {
57+
ERR_INVALID_ARG_TYPE,
58+
ERR_OUT_OF_RANGE,
59+
ERR_UNHANDLED_ERROR
60+
},
5761
} = require('internal/errors');
58-
const {
59-
ERR_INVALID_ARG_TYPE,
60-
ERR_OUT_OF_RANGE,
61-
ERR_UNHANDLED_ERROR
62-
} = codes;
6362

6463
const {
6564
inspect
@@ -76,14 +75,6 @@ const kMaxEventTargetListeners = Symbol('events.maxEventTargetListeners');
7675
const kMaxEventTargetListenersWarned =
7776
Symbol('events.maxEventTargetListenersWarned');
7877

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-
8778
function EventEmitter(opts) {
8879
FunctionPrototypeCall(EventEmitter.init, this, opts);
8980
}
@@ -713,7 +704,7 @@ async function once(emitter, name, options = {}) {
713704
const signal = options?.signal;
714705
validateAbortSignal(signal, 'options.signal');
715706
if (signal?.aborted)
716-
throw lazyDOMException('The operation was aborted', 'AbortError');
707+
throw new AbortError();
717708
return new Promise((resolve, reject) => {
718709
const errorListener = (err) => {
719710
emitter.removeListener(name, resolver);
@@ -738,7 +729,7 @@ async function once(emitter, name, options = {}) {
738729
function abortListener() {
739730
eventTargetAgnosticRemoveListener(emitter, name, resolver);
740731
eventTargetAgnosticRemoveListener(emitter, 'error', errorListener);
741-
reject(lazyDOMException('The operation was aborted', 'AbortError'));
732+
reject(new AbortError());
742733
}
743734
if (signal != null) {
744735
eventTargetAgnosticAddListener(
@@ -783,9 +774,8 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
783774
function on(emitter, event, options) {
784775
const signal = options?.signal;
785776
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();
789779

790780
const unconsumedEvents = [];
791781
const unconsumedPromises = [];
@@ -873,7 +863,7 @@ function on(emitter, event, options) {
873863
return iterator;
874864

875865
function abortListener() {
876-
errorHandler(lazyDOMException('The operation was aborted', 'AbortError'));
866+
errorHandler(new AbortError());
877867
}
878868

879869
function eventHandler(...args) {

‎lib/fs.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const {
8080
ERR_INVALID_ARG_TYPE,
8181
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM,
8282
},
83-
hideStackFrames,
83+
AbortError,
8484
uvErrmapGet,
8585
uvException
8686
} = require('internal/errors');
@@ -148,13 +148,6 @@ let ReadStream;
148148
let WriteStream;
149149
let rimraf;
150150
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-
});
158151

159152
// These have to be separate because of how graceful-fs happens to do it's
160153
// monkeypatching.
@@ -324,6 +317,14 @@ function readFileAfterStat(err, stats) {
324317
context.read();
325318
}
326319

320+
function checkAborted(signal, callback) {
321+
if (signal?.aborted) {
322+
callback(new AbortError());
323+
return true;
324+
}
325+
return false;
326+
}
327+
327328
function readFile(path, options, callback) {
328329
callback = maybeCallback(callback || options);
329330
options = getOptions(options, { flag: 'r' });
@@ -342,10 +343,8 @@ function readFile(path, options, callback) {
342343
return;
343344
}
344345

345-
if (options.signal?.aborted) {
346-
callback(lazyDOMException('The operation was aborted', 'AbortError'));
346+
if (checkAborted(options.signal, callback))
347347
return;
348-
}
349348

350349
const flagsNumber = stringToFlags(options.flag);
351350
path = getValidatedPath(path);
@@ -1459,10 +1458,10 @@ function lutimesSync(path, atime, mtime) {
14591458
function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) {
14601459
if (signal?.aborted) {
14611460
if (isUserFd) {
1462-
callback(lazyDOMException('The operation was aborted', 'AbortError'));
1461+
callback(new AbortError());
14631462
} else {
14641463
fs.close(fd, function() {
1465-
callback(lazyDOMException('The operation was aborted', 'AbortError'));
1464+
callback(new AbortError());
14661465
});
14671466
}
14681467
return;
@@ -1508,10 +1507,9 @@ function writeFile(path, data, options, callback) {
15081507
return;
15091508
}
15101509

1511-
if (options.signal?.aborted) {
1512-
callback(lazyDOMException('The operation was aborted', 'AbortError'));
1510+
if (checkAborted(options.signal, callback))
15131511
return;
1514-
}
1512+
15151513
fs.open(path, flag, options.mode, (openErr, fd) => {
15161514
if (openErr) {
15171515
callback(openErr);

‎lib/internal/fs/promises.js

+20-31
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ const {
3333
const binding = internalBinding('fs');
3434
const { Buffer } = require('buffer');
3535

36-
const { codes, hideStackFrames } = require('internal/errors');
3736
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');
4345
const { isArrayBufferView } = require('internal/util/types');
4446
const { rimrafPromises } = require('internal/fs/rimraf');
4547
const {
@@ -93,13 +95,6 @@ const {
9395
const getDirectoryEntriesPromise = promisify(getDirents);
9496
const validateRmOptionsPromise = promisify(validateRmOptions);
9597

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-
10398
class FileHandle extends EventEmitterMixin(JSTransferable) {
10499
constructor(filehandle) {
105100
super();
@@ -272,15 +267,18 @@ async function fsCall(fn, handle, ...args) {
272267
}
273268
}
274269

270+
function checkAborted(signal) {
271+
if (signal?.aborted)
272+
throw new AbortError();
273+
}
274+
275275
async function writeFileHandle(filehandle, data, signal) {
276276
// `data` could be any kind of typed array.
277277
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
278278
let remaining = data.length;
279279
if (remaining === 0) return;
280280
do {
281-
if (signal?.aborted) {
282-
throw lazyDOMException('The operation was aborted', 'AbortError');
283-
}
281+
checkAborted(signal);
284282
const { bytesWritten } =
285283
await write(filehandle, data, 0,
286284
MathMin(kWriteFileMaxChunkSize, data.length));
@@ -296,14 +294,11 @@ async function writeFileHandle(filehandle, data, signal) {
296294
async function readFileHandle(filehandle, options) {
297295
const signal = options?.signal;
298296

299-
if (signal?.aborted) {
300-
throw lazyDOMException('The operation was aborted', 'AbortError');
301-
}
297+
checkAborted(signal);
298+
302299
const statFields = await binding.fstat(filehandle.fd, false, kUsePromises);
303300

304-
if (signal?.aborted) {
305-
throw lazyDOMException('The operation was aborted', 'AbortError');
306-
}
301+
checkAborted(signal);
307302

308303
let size;
309304
if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) {
@@ -321,9 +316,7 @@ async function readFileHandle(filehandle, options) {
321316
const buffers = [];
322317
const fullBuffer = noSize ? undefined : Buffer.allocUnsafeSlow(size);
323318
do {
324-
if (signal?.aborted) {
325-
throw lazyDOMException('The operation was aborted', 'AbortError');
326-
}
319+
checkAborted(signal);
327320
let buffer;
328321
let offset;
329322
let length;
@@ -693,9 +686,7 @@ async function writeFile(path, data, options) {
693686
if (path instanceof FileHandle)
694687
return writeFileHandle(path, data, options.signal);
695688

696-
if (options.signal?.aborted) {
697-
throw lazyDOMException('The operation was aborted', 'AbortError');
698-
}
689+
checkAborted(options.signal);
699690

700691
const fd = await open(path, flag, options.mode);
701692
const { signal } = options;
@@ -716,9 +707,7 @@ async function readFile(path, options) {
716707
if (path instanceof FileHandle)
717708
return readFileHandle(path, options);
718709

719-
if (options.signal?.aborted) {
720-
throw lazyDOMException('The operation was aborted', 'AbortError');
721-
}
710+
checkAborted(options.signal);
722711

723712
const fd = await open(path, flag, 0o666);
724713
return PromisePrototypeFinally(readFileHandle(fd, options), fd.close);

‎lib/internal/fs/read_file_context.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,9 @@ const { Buffer } = require('buffer');
1010

1111
const { FSReqCallback, close, read } = internalBinding('fs');
1212

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');
2216

2317
// Use 64kb in case the file type is not a regular file and thus do not know the
2418
// actual file size. Increasing the value further results in more frequent over
@@ -95,9 +89,7 @@ class ReadFileContext {
9589
let length;
9690

9791
if (this.signal?.aborted) {
98-
return this.close(
99-
lazyDOMException('The operation was aborted', 'AbortError')
100-
);
92+
return this.close(new AbortError());
10193
}
10294
if (this.size === 0) {
10395
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);

0 commit comments

Comments
 (0)
Please sign in to comment.