Skip to content

Commit 12fb2ff

Browse files
jasnelldanielleadams
authored andcommitted
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

4 files changed

Lines changed: 49 additions & 80 deletions

File tree

‎lib/events.js‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ const kRejection = SymbolFor('nodejs.rejection');
5151
letspliceOne;
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
constkMaxEventTargetListenersWarned=
7776
Symbol('events.maxEventTargetListenersWarned');
7877

79-
letDOMException;
80-
constlazyDOMException=hideStackFrames((message,name)=>{
81-
if(DOMException===undefined)
82-
DOMException=internalBinding('messaging').DOMException;
83-
returnnewDOMException(message,name);
84-
});
85-
86-
8778
functionEventEmitter(opts){
8879
FunctionPrototypeCall(EventEmitter.init,this,opts);
8980
}
@@ -713,7 +704,7 @@ async function once(emitter, name, options = {}) {
713704
constsignal=options?.signal;
714705
validateAbortSignal(signal,'options.signal');
715706
if(signal?.aborted)
716-
throwlazyDOMException('The operation was aborted','AbortError');
707+
thrownewAbortError();
717708
returnnewPromise((resolve,reject)=>{
718709
consterrorListener=(err)=>{
719710
emitter.removeListener(name,resolver);
@@ -738,7 +729,7 @@ async function once(emitter, name, options = {}) {
738729
functionabortListener(){
739730
eventTargetAgnosticRemoveListener(emitter,name,resolver);
740731
eventTargetAgnosticRemoveListener(emitter,'error',errorListener);
741-
reject(lazyDOMException('The operation was aborted','AbortError'));
732+
reject(newAbortError());
742733
}
743734
if(signal!=null){
744735
eventTargetAgnosticAddListener(
@@ -783,9 +774,8 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
783774
functionon(emitter,event,options){
784775
constsignal=options?.signal;
785776
validateAbortSignal(signal,'options.signal');
786-
if(signal?.aborted){
787-
throwlazyDOMException('The operation was aborted','AbortError');
788-
}
777+
if(signal?.aborted)
778+
thrownewAbortError();
789779

790780
constunconsumedEvents=[];
791781
constunconsumedPromises=[];
@@ -873,7 +863,7 @@ function on(emitter, event, options) {
873863
returniterator;
874864

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

879869
functioneventHandler(...args){

‎lib/fs.js‎

Lines changed: 14 additions & 16 deletions
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
letWriteStream;
149149
letrimraf;
150150
letrimrafSync;
151-
letDOMException;
152-
153-
constlazyDOMException=hideStackFrames((message,name)=>{
154-
if(DOMException===undefined)
155-
DOMException=internalBinding('messaging').DOMException;
156-
returnnewDOMException(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+
functioncheckAborted(signal,callback){
321+
if(signal?.aborted){
322+
callback(newAbortError());
323+
returntrue;
324+
}
325+
returnfalse;
326+
}
327+
327328
functionreadFile(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
constflagsNumber=stringToFlags(options.flag);
351350
path=getValidatedPath(path);
@@ -1459,10 +1458,10 @@ function lutimesSync(path, atime, mtime) {
14591458
functionwriteAll(fd,isUserFd,buffer,offset,length,signal,callback){
14601459
if(signal?.aborted){
14611460
if(isUserFd){
1462-
callback(lazyDOMException('The operation was aborted','AbortError'));
1461+
callback(newAbortError());
14631462
}else{
14641463
fs.close(fd,function(){
1465-
callback(lazyDOMException('The operation was aborted','AbortError'));
1464+
callback(newAbortError());
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‎

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ const {
3333
constbinding=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
constgetDirectoryEntriesPromise=promisify(getDirents);
9496
constvalidateRmOptionsPromise=promisify(validateRmOptions);
9597

96-
letDOMException;
97-
constlazyDOMException=hideStackFrames((message,name)=>{
98-
if(DOMException===undefined)
99-
DOMException=internalBinding('messaging').DOMException;
100-
returnnewDOMException(message,name);
101-
});
102-
10398
classFileHandleextendsEventEmitterMixin(JSTransferable){
10499
constructor(filehandle){
105100
super();
@@ -272,15 +267,18 @@ async function fsCall(fn, handle, ...args) {
272267
}
273268
}
274269

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

299-
if(signal?.aborted){
300-
throwlazyDOMException('The operation was aborted','AbortError');
301-
}
297+
checkAborted(signal);
298+
302299
conststatFields=awaitbinding.fstat(filehandle.fd,false,kUsePromises);
303300

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

308303
letsize;
309304
if((statFields[1/* mode */]&S_IFMT)===S_IFREG){
@@ -321,9 +316,7 @@ async function readFileHandle(filehandle, options) {
321316
constbuffers=[];
322317
constfullBuffer=noSize ? undefined : Buffer.allocUnsafeSlow(size);
323318
do{
324-
if(signal?.aborted){
325-
throwlazyDOMException('The operation was aborted','AbortError');
326-
}
319+
checkAborted(signal);
327320
letbuffer;
328321
letoffset;
329322
letlength;
@@ -693,9 +686,7 @@ async function writeFile(path, data, options) {
693686
if(pathinstanceofFileHandle)
694687
returnwriteFileHandle(path,data,options.signal);
695688

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

700691
constfd=awaitopen(path,flag,options.mode);
701692
const{ signal }=options;
@@ -716,9 +707,7 @@ async function readFile(path, options) {
716707
if(pathinstanceofFileHandle)
717708
returnreadFileHandle(path,options);
718709

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

723712
constfd=awaitopen(path,flag,0o666);
724713
returnPromisePrototypeFinally(readFileHandle(fd,options),fd.close);

‎lib/internal/fs/read_file_context.js‎

Lines changed: 4 additions & 12 deletions
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-
letDOMException;
17-
constlazyDOMException=hideStackFrames((message,name)=>{
18-
if(DOMException===undefined)
19-
DOMException=internalBinding('messaging').DOMException;
20-
returnnewDOMException(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
letlength;
9690

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

0 commit comments

Comments
 (0)