Skip to content

Commit

Permalink
refactor: Errors do not need "new"
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Sep 7, 2024
1 parent 79e90eb commit e5dae98
Show file tree
Hide file tree
Showing 47 changed files with 118 additions and 118 deletions.
4 changes: 2 additions & 2 deletions packages/cli/demo/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ const render = value => {
return $value;
}
default: {
throw new Error(
throw Error(
'Unreachable if programmed to account for all pass-styles',
);
}
Expand Down Expand Up @@ -908,7 +908,7 @@ const evalComponent = ($parent, powers, { dismissEval, showValue }) => {
const id = target.getAttribute('id');
const $endowment = $endowments.get(id);
if ($endowment === undefined) {
throw new Error(`Endowment does not exist for id ${id}`);
throw Error(`Endowment does not exist for id ${id}`);
}
$endowments.delete(id);
$endowment.remove();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const evalCommand = async ({
return [name, name];
}
if (pair.length > 2) {
throw new Error(
throw Error(
`Specify either a name endowmentName:pet-name, got: ${JSON.stringify(
name,
)}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/pet-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const parsePetNamePath = petNamePath => {
const petNames = petNamePath.split('.');
for (const petName of petNames) {
if (petName === '') {
throw new Error(
throw Error(
`Pet name path ${q(petNamePath)} contains an empty segment.`,
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compartment-mapper/src/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export const makeBundle = async (readPowers, moduleLocation, options) => {
}
const module = modulesByKey[key];
if (module === undefined) {
throw new Error(
throw Error(
`Unable to locate module for key ${q(key)} import specifier ${q(
importSpecifier,
)} in ${q(module.moduleSpecifier)} of compartment ${q(
Expand Down
2 changes: 1 addition & 1 deletion packages/compartment-mapper/src/node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ const translateGraph = (
/* c8 ignore next */
if (policy && !packagePolicy) {
// this should never happen
throw new TypeError('Unexpectedly falsy package policy');
throw TypeError('Unexpectedly falsy package policy');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const wrap = ({

const require = (/** @type {string} */ importSpecifier) => {
if (!has(resolvedImports, importSpecifier)) {
throw new Error(
throw Error(
`Cannot find module "${importSpecifier}" in "${location}"`,
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/compartment-mapper/test/custom-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test('a custom parser may implement policy enforcement (allowed)', async t => {
compartmentDescriptor.policy.options &&
compartmentDescriptor.policy.options.markdown !== true)
) {
throw new Error(`Markdown parsing not allowed for ${q(specifier)}`);
throw Error(`Markdown parsing not allowed for ${q(specifier)}`);
}

return {
Expand Down Expand Up @@ -173,7 +173,7 @@ test('a custom parser may implement policy enforcement (disallowed)', async t =>
compartmentDescriptor.policy.options &&
compartmentDescriptor.policy.options.markdown !== true)
) {
throw new Error(`Markdown parsing not allowed for ${q(specifier)}`);
throw Error(`Markdown parsing not allowed for ${q(specifier)}`);
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { meaning } from './1.cjs';
if (meaning !== 42) {
throw new Error('Fail');
throw Error('Fail');
}
2 changes: 1 addition & 1 deletion packages/compartment-mapper/test/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export function scaffold(
// in a way that is difficult to generalize since not all test paths
// reach here.
if (sourceMaps.size !== 0) {
throw new Error(
throw Error(
'The bundler and importer should agree on source map count',
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compartment-mapper/test/search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const makeFakeReadPowers = files => {
async read(location) {
const bytes = files[location];
if (bytes === undefined) {
throw new Error(`File not found: ${location}`);
throw Error(`File not found: ${location}`);
}
return bytes;
},
Expand Down
4 changes: 2 additions & 2 deletions packages/daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const removePath = async removalPath => {
.rm(removalPath, { recursive: true, force: true })
.catch(cause => {
/** @type {object} */
const error = new Error(cause.message, { cause });
const error = Error(cause.message, { cause });
error.code = cause.code;
throw error;
});
Expand Down Expand Up @@ -129,7 +129,7 @@ export const start = async (config = defaultConfig) => {
'message' in message &&
typeof message.message === 'string'
) {
reject(new Error(message.message));
reject(Error(message.message));
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const makeEndoClient = async (name, sockPath, cancelled, bootstrap) => {
conn.on('error', (/** @type {any} */ error) => {
if (error.code === 'ENOENT') {
reject(
new Error(
Error(
`Cannot connect to Endo. Is Endo running? ${error.message}`,
),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const makeContextMaker = ({ controllerForId, provideController }) => {
const cancel = (reason, prefix = '*') => {
if (done) return disposed;
done = true;
rejectCancelled(reason || harden(new Error('Cancelled')));
rejectCancelled(reason || harden(Error('Cancelled')));

console.log(`${prefix} ${id}`);

Expand Down
6 changes: 3 additions & 3 deletions packages/daemon/src/daemon-node-powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ export const makeDaemonicPersistencePowers = (
const makeFormulaPath = formulaNumber => {
const { statePath } = config;
if (formulaNumber.length < 3) {
throw new TypeError(`Invalid formula number ${q(formulaNumber)}`);
throw TypeError(`Invalid formula number ${q(formulaNumber)}`);
}
const head = formulaNumber.slice(0, 2);
const tail = formulaNumber.slice(2);
Expand All @@ -403,13 +403,13 @@ export const makeDaemonicPersistencePowers = (
const { file: formulaPath } = makeFormulaPath(formulaNumber);
const formulaText = await filePowers.maybeReadFileText(formulaPath);
if (formulaText === undefined) {
throw new ReferenceError(`No reference exists at path ${formulaPath}`);
throw ReferenceError(`No reference exists at path ${formulaPath}`);
}
const formula = (() => {
try {
return JSON.parse(formulaText);
} catch (error) {
throw new TypeError(
throw TypeError(
`Corrupt description for reference in file ${formulaPath}: ${error.message}`,
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/daemon/src/daemon-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
/** @import { Config, Builtins } from './types.js' */

if (process.argv.length < 5) {
throw new Error(
throw Error(
`daemon.js requires arguments [sockPath] [statePath] [ephemeralStatePath] [cachePath], got ${process.argv.join(
', ',
)}`,
Expand Down Expand Up @@ -144,11 +144,11 @@ const main = async () => {

// Wait for services to end normally
await servicesStopped;
cancel(new Error('Terminated normally'));
cancelGracePeriod(new Error('Terminated normally'));
cancel(Error('Terminated normally'));
cancelGracePeriod(Error('Terminated normally'));
};

process.once('SIGINT', () => cancel(new Error('SIGINT')));
process.once('SIGINT', () => cancel(Error('SIGINT')));

// @ts-ignore Yes, we can assign to exitCode, typedoc.
process.exitCode = 1;
Expand Down
22 changes: 11 additions & 11 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ const makeDaemonCore = async (
provide: async requestedId => {
const { node } = parseId(requestedId);
if (node !== localNodeId) {
throw new Error(
throw Error(
`Gateway can only provide local values. Got request for node ${q(
node,
)}`,
Expand Down Expand Up @@ -349,15 +349,15 @@ const makeDaemonCore = async (
const gracefulCancel = async () => {
E.sendOnly(workerDaemonFacet).terminate();
const cancelWorkerGracePeriod = () => {
throw new Error('Exited gracefully before grace period elapsed');
throw Error('Exited gracefully before grace period elapsed');
};
const workerGracePeriodCancelled = Promise.race([
gracePeriodElapsed,
workerTerminated,
]).then(cancelWorkerGracePeriod, cancelWorkerGracePeriod);
await delay(gracePeriodMs, workerGracePeriodCancelled)
.then(() => {
throw new Error(
throw Error(
`Worker termination grace period ${gracePeriodMs}ms elapsed`,
);
})
Expand Down Expand Up @@ -599,7 +599,7 @@ const makeDaemonCore = async (
const endoBootstrap = makeExo('Endo', EndoInterface, {
ping: async () => 'pong',
terminate: async () => {
cancel(new Error('Termination requested'));
cancel(Error('Termination requested'));
},
host: () => provide(hostId, 'host'),
leastAuthority: () => provide(leastAuthorityId, 'guest'),
Expand Down Expand Up @@ -631,7 +631,7 @@ const makeDaemonCore = async (
makeLoopbackNetwork(Promise.resolve(localGateway)),
'least-authority': () => {
const disallowedFn = async () => {
throw new Error('not allowed');
throw Error('not allowed');
};
return /** @type {FarRef<EndoGuest>} */ (
/** @type {unknown} */ (
Expand Down Expand Up @@ -722,7 +722,7 @@ const makeDaemonCore = async (
}
return value;
} else {
throw new TypeError(`Invalid formula: ${q(formula)}`);
throw TypeError(`Invalid formula: ${q(formula)}`);
}
};

Expand Down Expand Up @@ -818,12 +818,12 @@ const makeDaemonCore = async (
*/
const getPeerIdForNodeIdentifier = async nodeId => {
if (nodeId === localNodeId) {
throw new Error(`Cannot get peer formula identifier for self`);
throw Error(`Cannot get peer formula identifier for self`);
}
const knownPeers = await provide(knownPeersId, 'pet-store');
const peerId = knownPeers.identifyLocal(nodeId);
if (peerId === undefined) {
throw new Error(`No peer found for node identifier ${q(nodeId)}.`);
throw Error(`No peer found for node identifier ${q(nodeId)}.`);
}
return peerId;
};
Expand Down Expand Up @@ -1541,7 +1541,7 @@ const makeDaemonCore = async (
}
}
}
throw new Error('Cannot connect to peer: no supported addresses');
throw Error('Cannot connect to peer: no supported addresses');
},
context.cancel,
context.cancelled,
Expand Down Expand Up @@ -1602,7 +1602,7 @@ const makeDaemonCore = async (
await formulaGraphJobs.enqueue();
const controller = provideController(id);
console.log('Cancelled:');
await controller.context.cancel(new Error('Invitation accepted'));
await controller.context.cancel(Error('Invitation accepted'));

await E(hostAgent).write([guestName], guestHandleId);

Expand Down Expand Up @@ -1679,7 +1679,7 @@ const makeDaemonCore = async (
}
const id = petStore.identifyLocal(petName);
if (id === undefined) {
throw new Error(`Unknown pet name ${petName}`);
throw Error(`Unknown pet name ${petName}`);
}
const { number: formulaNumber } = parseId(id);
const formula = await getFormulaForId(id);
Expand Down
8 changes: 4 additions & 4 deletions packages/daemon/src/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const makeDirectoryMaker = ({

const id = petStore.identifyLocal(headName);
if (id === undefined) {
throw new TypeError(`Unknown pet name: ${q(headName)}`);
throw TypeError(`Unknown pet name: ${q(headName)}`);
}
const value = provide(id, 'hub');
return tailNames.reduce(
Expand All @@ -59,7 +59,7 @@ export const makeDirectoryMaker = ({
*/
const lookupTailNameHub = async petNamePath => {
if (petNamePath.length === 0) {
throw new TypeError(`Empty pet name path`);
throw TypeError(`Empty pet name path`);
}
const headPath = petNamePath.slice(0, -1);
const tailName = petNamePath[petNamePath.length - 1];
Expand Down Expand Up @@ -193,7 +193,7 @@ export const makeDirectoryMaker = ({

const id = await E(fromHub).identify(fromName);
if (id === undefined) {
throw new Error(`Unknown name: ${q(fromPath)}`);
throw Error(`Unknown name: ${q(fromPath)}`);
}
// First write to the "to" hub so that the original name is preserved on the
// "from" hub in case of failure.
Expand All @@ -208,7 +208,7 @@ export const makeDirectoryMaker = ({
const { hub: toHub, name: toName } = await lookupTailNameHub(toPath);
const id = await fromHub.identify(fromName);
if (id === undefined) {
throw new Error(`Unknown name: ${q(fromPath)}`);
throw Error(`Unknown name: ${q(fromPath)}`);
}
await toHub.write([toName], id);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/daemon/src/formula-identifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const assertValidId = (id, petName) => {
if (petName !== undefined) {
message += ` for pet name ${q(petName)}`;
}
throw new Error(message);
throw Error(message);
}
};

Expand Down
12 changes: 6 additions & 6 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const makeHostMaker = ({
assertPetNamePath(resultName);
}
if (petNamePaths.length !== codeNames.length) {
throw new Error('Evaluator requires one pet name for each code name');
throw Error('Evaluator requires one pet name for each code name');
}

/** @type {DeferredTasks<EvalDeferredTaskParams>} */
Expand All @@ -207,14 +207,14 @@ export const makeHostMaker = ({
const endowmentFormulaIdsOrPaths = petNamePaths.map(
(petNameOrPath, index) => {
if (typeof codeNames[index] !== 'string') {
throw new Error(`Invalid endowment name: ${q(codeNames[index])}`);
throw Error(`Invalid endowment name: ${q(codeNames[index])}`);
}

const petNamePath = petNamePathFrom(petNameOrPath);
if (petNamePath.length === 1) {
const id = petStore.identifyLocal(petNamePath[0]);
if (id === undefined) {
throw new Error(`Unknown pet name ${q(petNamePath[0])}`);
throw Error(`Unknown pet name ${q(petNamePath[0])}`);
}
return id;
}
Expand Down Expand Up @@ -310,7 +310,7 @@ export const makeHostMaker = ({
) => {
const bundleId = petStore.identifyLocal(bundleName);
if (bundleId === undefined) {
throw new TypeError(`Unknown pet name for bundle: ${q(bundleName)}`);
throw TypeError(`Unknown pet name for bundle: ${q(bundleName)}`);
}

const { tasks, workerId, powersId } = prepareMakeCaplet(
Expand Down Expand Up @@ -538,10 +538,10 @@ export const makeHostMaker = ({
};

/** @type {EndoHost['cancel']} */
const cancel = async (petName, reason = new Error('Cancelled')) => {
const cancel = async (petName, reason = Error('Cancelled')) => {
const id = petStore.identifyLocal(petName);
if (id === undefined) {
throw new TypeError(`Unknown pet name: ${q(petName)}`);
throw TypeError(`Unknown pet name: ${q(petName)}`);
}
return cancelValue(id, reason);
};
Expand Down
Loading

0 comments on commit e5dae98

Please sign in to comment.