Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Errors do not need "new" #2445

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/cli/demo/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,7 @@ const render = value => {
return $value;
}
default: {
throw new Error(
'Unreachable if programmed to account for all pass-styles',
);
throw Error('Unreachable if programmed to account for all pass-styles');
}
}
};
Expand Down Expand Up @@ -908,7 +906,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
4 changes: 1 addition & 3 deletions packages/cli/src/pet-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export const parsePetNamePath = petNamePath => {
const petNames = petNamePath.split('.');
for (const petName of petNames) {
if (petName === '') {
throw new Error(
`Pet name path ${q(petNamePath)} contains an empty segment.`,
);
throw Error(`Pet name path ${q(petNamePath)} contains an empty segment.`);
}
}
return petNames;
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 @@ -358,7 +358,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 @@ -691,7 +691,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 @@ -158,7 +158,7 @@ export const wrap = ({
namespace = compartment.importNow(importSpecifier);
} else {
const invalidProps = findInvalidReadNowPowersProps(readPowers).sort();
throw new Error(
throw Error(
`Synchronous readPowers required for dynamic import of ${assert.quote(importSpecifier)}; missing or invalid prop(s): ${invalidProps.join(', ')}`,
);
}
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 @@ -477,7 +477,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
4 changes: 1 addition & 3 deletions packages/daemon/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export const makeEndoClient = async (name, sockPath, cancelled, bootstrap) => {
conn.on('error', (/** @type {any} */ error) => {
if (error.code === 'ENOENT') {
reject(
new Error(
`Cannot connect to Endo. Is Endo running? ${error.message}`,
),
Error(`Cannot connect to Endo. Is Endo running? ${error.message}`),
);
} else {
reject(error);
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 @@ -284,7 +284,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 @@ -347,15 +347,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 @@ -597,7 +597,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 @@ -629,7 +629,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 @@ -720,7 +720,7 @@ const makeDaemonCore = async (
}
return value;
} else {
throw new TypeError(`Invalid formula: ${q(formula)}`);
throw TypeError(`Invalid formula: ${q(formula)}`);
}
};

Expand Down Expand Up @@ -816,12 +816,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 @@ -1539,7 +1539,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 @@ -1600,7 +1600,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 @@ -1677,7 +1677,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
Loading