Skip to content

Commit 4b02cd5

Browse files
refactor: improve maintainability (#467)
* refactor: import node built-in modules using "node:" prefix * test: skip test relying on external RPC providers * refactor: consistently call Error constructor with "new"
1 parent 9f64d1c commit 4b02cd5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+468
-418
lines changed

src/cli/cmd/iexec-account.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ show
108108
userAddress = userWalletAddress;
109109
spinner.info(`Current account address ${userWalletAddress}`);
110110
} else {
111-
throw Error('Wallet file not found');
111+
throw new Error('Wallet file not found');
112112
}
113113
} catch (error) {
114-
throw Error(
114+
throw new Error(
115115
`Failed to load wallet address from keystore: ${error.message}`,
116116
);
117117
}
118118
} else {
119119
userAddress = address;
120120
}
121-
if (!userAddress) throw Error('Missing address or wallet');
121+
if (!userAddress) throw new Error('Missing address or wallet');
122122

123123
const chain = await loadChain(opts.chain, { spinner });
124124

src/cli/cmd/iexec-app.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ deploy
197197
loadIExecConf(),
198198
]);
199199
if (!iexecConf[objName]) {
200-
throw Error(
200+
throw new Error(
201201
`Missing ${objName} in "iexec.json". Did you forget to run "iexec ${objName} init"?`,
202202
);
203203
}
@@ -242,10 +242,10 @@ show
242242
const isAddress = isEthAddress(addressOrIndex, { strict: false });
243243
const userAddress = opts.user || (address !== NULL_ADDRESS && address);
244244
if (!isAddress && !userAddress)
245-
throw Error(`Missing option ${option.user()[0]} or wallet`);
245+
throw new Error(`Missing option ${option.user()[0]} or wallet`);
246246

247247
if (!addressOrIndex)
248-
throw Error(info.missingAddressOrDeployed(objName, chain.id));
248+
throw new Error(info.missingAddressOrDeployed(objName, chain.id));
249249
spinner.start(info.showing(objName));
250250

251251
let res;
@@ -307,7 +307,7 @@ count
307307
]);
308308
const userAddress = opts.user || (address !== NULL_ADDRESS && address);
309309
if (!userAddress)
310-
throw Error(`Missing option ${option.user()[0]} or wallet`);
310+
throw new Error(`Missing option ${option.user()[0]} or wallet`);
311311
spinner.start(info.counting(objName));
312312
const objCountBN = await countUserApps(chain.contracts, userAddress);
313313
spinner.succeed(
@@ -336,7 +336,7 @@ checkSecret
336336
(deployedObj) => deployedObj && deployedObj[chain.id],
337337
));
338338
if (!resourceAddress) {
339-
throw Error(
339+
throw new Error(
340340
'Missing appAddress argument and no app found in "deployed.json"',
341341
);
342342
}
@@ -389,7 +389,7 @@ pushSecret
389389
(deployedObj) => deployedObj && deployedObj[chain.id],
390390
));
391391
if (!resourceAddress) {
392-
throw Error(
392+
throw new Error(
393393
'Missing appAddress argument and no app found in "deployed.json"',
394394
);
395395
}
@@ -418,7 +418,7 @@ pushSecret
418418
raw: {},
419419
});
420420
} else {
421-
throw Error('Something went wrong');
421+
throw new Error('Something went wrong');
422422
}
423423
} catch (error) {
424424
handleError(error, cli, opts);
@@ -453,7 +453,7 @@ publish
453453
(deployedObj) => deployedObj && deployedObj[chain.id],
454454
));
455455
if (!address) {
456-
throw Error(info.missingAddressOrDeployed(objName, chain.id));
456+
throw new Error(info.missingAddressOrDeployed(objName, chain.id));
457457
}
458458
debug('useDeployedObj', useDeployedObj, 'address', address);
459459
if (useDeployedObj) {
@@ -463,7 +463,7 @@ publish
463463
}
464464
spinner.info(`Creating ${objName}order for ${objName} ${address}`);
465465
if (!(await checkDeployedApp(chain.contracts, address))) {
466-
throw Error(`No ${objName} deployed at address ${address}`);
466+
throw new Error(`No ${objName} deployed at address ${address}`);
467467
}
468468
const overrides = {
469469
app: address,
@@ -523,7 +523,7 @@ unpublish
523523
(deployedObj) => deployedObj && deployedObj[chain.id],
524524
));
525525
if (!address) {
526-
throw Error(info.missingAddressOrDeployed(objName, chain.id));
526+
throw new Error(info.missingAddressOrDeployed(objName, chain.id));
527527
}
528528
debug('useDeployedObj', useDeployedObj, 'address', address);
529529
if (useDeployedObj) {
@@ -604,7 +604,7 @@ run
604604
(deployedApp) => deployedApp && deployedApp[chain.id],
605605
));
606606
if (!app) {
607-
throw Error(
607+
throw new Error(
608608
`Missing appAddress and no app found in "deployed.json" for chain ${chain.id}`,
609609
);
610610
}
@@ -625,7 +625,7 @@ run
625625
)
626626
: opts.dataset);
627627
if (useDataset && !dataset) {
628-
throw Error(
628+
throw new Error(
629629
`No dataset found in "deployed.json" for chain ${chain.id}`,
630630
);
631631
}
@@ -653,7 +653,7 @@ run
653653
)
654654
: opts.workerpool);
655655
if (runOnWorkerpool && !workerpool) {
656-
throw Error(
656+
throw new Error(
657657
`No workerpool found in "deployed.json" for chain ${chain.id}`,
658658
);
659659
}
@@ -748,7 +748,7 @@ run
748748
const getApporder = async () => {
749749
spinner.info(`Using app ${app}`);
750750
if (!(await checkDeployedApp(chain.contracts, app)))
751-
throw Error(`No app deployed at address ${app}`);
751+
throw new Error(`No app deployed at address ${app}`);
752752
const appOwner = await getAppOwner(chain.contracts, app);
753753
const isAppOwner = appOwner.toLowerCase() === requester.toLowerCase();
754754
if (isAppOwner) {
@@ -785,7 +785,7 @@ run
785785
},
786786
);
787787
const order = orders[0] && orders[0].order;
788-
if (!order) throw Error(`No order available for app ${app}`);
788+
if (!order) throw new Error(`No order available for app ${app}`);
789789
return order;
790790
};
791791

@@ -800,7 +800,7 @@ run
800800
)
801801
return NULL_DATASETORDER;
802802
if (!(await checkDeployedDataset(chain.contracts, dataset)))
803-
throw Error(`No dataset deployed at address ${dataset}`);
803+
throw new Error(`No dataset deployed at address ${dataset}`);
804804
const datasetOwner = await getDatasetOwner(chain.contracts, dataset);
805805
const isDatasetOwner =
806806
datasetOwner.toLowerCase() === requester.toLowerCase();
@@ -828,7 +828,8 @@ run
828828
},
829829
);
830830
const order = orders[0] && orders[0].order;
831-
if (!order) throw Error(`No order available for dataset ${dataset}`);
831+
if (!order)
832+
throw new Error(`No order available for dataset ${dataset}`);
832833
return order;
833834
};
834835

@@ -850,7 +851,7 @@ run
850851
debug('minTag', minTag);
851852
if (runOnWorkerpool) {
852853
if (!(await checkDeployedWorkerpool(chain.contracts, workerpool)))
853-
throw Error(`No workerpool deployed at address ${workerpool}`);
854+
throw new Error(`No workerpool deployed at address ${workerpool}`);
854855
const workerpoolOwner = await getWorkerpoolOwner(
855856
chain.contracts,
856857
workerpool,
@@ -912,7 +913,7 @@ run
912913
return order;
913914
}
914915
if (strict) {
915-
throw Error(
916+
throw new Error(
916917
`No workerpoolorder matching your conditions available in category ${category}`,
917918
);
918919
}
@@ -921,7 +922,7 @@ run
921922
await showCategory(chain.contracts, nextCatid);
922923
} catch (error) {
923924
debug(error);
924-
throw Error(
925+
throw new Error(
925926
'No workerpoolorder matching your conditions currently available',
926927
);
927928
}
@@ -931,7 +932,9 @@ run
931932
strict: useCategory,
932933
});
933934
if (!order) {
934-
throw Error(`No workerpoolorder available in category ${category}`);
935+
throw new Error(
936+
`No workerpoolorder available in category ${category}`,
937+
);
935938
}
936939
return order;
937940
};
@@ -986,7 +989,7 @@ run
986989
apporder,
987990
{ tagOverride: resolvedTag },
988991
).catch((e) => {
989-
throw Error(
992+
throw new Error(
990993
`App requirements check failed: ${
991994
e.message
992995
} (If you consider this is not an issue, use ${
@@ -1004,7 +1007,7 @@ run
10041007
datasetorder,
10051008
{ tagOverride: resolvedTag },
10061009
).catch((e) => {
1007-
throw Error(
1010+
throw new Error(
10081011
`Dataset requirements check failed: ${
10091012
e.message
10101013
} (If you consider this is not an issue, use ${
@@ -1021,7 +1024,7 @@ run
10211024
},
10221025
requestorderToSign,
10231026
).catch((e) => {
1024-
throw Error(
1027+
throw new Error(
10251028
`Request requirements check failed: ${
10261029
e.message
10271030
} (If you consider this is not an issue, use ${
@@ -1191,23 +1194,23 @@ requestRun
11911194
const chain = await loadChain(opts.chain, { spinner });
11921195
debug('app', app);
11931196
if (!(await checkDeployedApp(chain.contracts, app))) {
1194-
throw Error(`No app deployed at address ${app}`);
1197+
throw new Error(`No app deployed at address ${app}`);
11951198
}
11961199
const dataset = opts.dataset || NULL_ADDRESS;
11971200
debug('dataset', dataset);
11981201
if (
11991202
dataset !== NULL_ADDRESS &&
12001203
!(await checkDeployedDataset(chain.contracts, dataset))
12011204
) {
1202-
throw Error(`No dataset deployed at address ${dataset}`);
1205+
throw new Error(`No dataset deployed at address ${dataset}`);
12031206
}
12041207
const workerpool = opts.workerpool || NULL_ADDRESS;
12051208
debug('workerpool', workerpool);
12061209
if (
12071210
workerpool !== NULL_ADDRESS &&
12081211
!(await checkDeployedWorkerpool(chain.contracts, workerpool))
12091212
) {
1210-
throw Error(`No workerpool deployed at address ${workerpool}`);
1213+
throw new Error(`No workerpool deployed at address ${workerpool}`);
12111214
}
12121215
const appprice = await nRlcAmountSchema().validate(opts.appPrice || 0);
12131216
debug('appprice', appprice);
@@ -1324,7 +1327,7 @@ requestRun
13241327
},
13251328
requestorderToSign,
13261329
).catch((e) => {
1327-
throw Error(
1330+
throw new Error(
13281331
`Request requirements check failed: ${
13291332
e.message
13301333
} (If you consider this is not an issue, use ${
@@ -1346,7 +1349,7 @@ requestRun
13461349

13471350
const { stake } = await checkBalance(chain.contracts, requester);
13481351
if (totalCost.gt(stake)) {
1349-
throw Error(
1352+
throw new Error(
13501353
`Not enough RLC on your account (${formatRLC(
13511354
totalCost,
13521355
)} RLC required). Run "iexec account deposit" to topup your account.`,
@@ -1431,7 +1434,7 @@ transfer
14311434
const keystore = Keystore(walletOptions);
14321435
const chain = await loadChain(opts.chain, { txOptions, spinner });
14331436
await connectKeystore(chain, keystore, { txOptions });
1434-
if (!opts.to) throw Error('Missing --to option');
1437+
if (!opts.to) throw new Error('Missing --to option');
14351438
if (!opts.force) {
14361439
await prompt.transferObj(objName, objAddress, opts.to, chain.id);
14371440
}

src/cli/cmd/iexec-category.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ create
6666
loadChain(opts.chain, { txOptions, spinner }),
6767
]);
6868
if (!iexecConf[objName]) {
69-
throw Error(
69+
throw new Error(
7070
`Missing ${objName} in "iexec.json". Did you forget to run "iexec ${objName} init"?`,
7171
);
7272
}

0 commit comments

Comments
 (0)