Skip to content

Commit

Permalink
add helpful error when initial data is a non-object
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed Mar 25, 2023
1 parent 2a05809 commit 0a1c34e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/swingset-liveslots/src/virtualObjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,10 @@ export function makeVirtualObjectManager(
// kdebug(`vo make ${baseRef}`);

const initialData = init ? init(...args) : {};
if (typeof initialData !== 'object') {
Fail`initial data must be object, not ${initialData}`;
// a common mistake is to use `() => {foo:1}`, not `() => ({foo:1})`
}

// save (i.e. populate the cache) with the initial serialized record
const capdatas = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'ava';
import '@endo/init/debug.js';

import { makeFakeVirtualObjectManager } from '../../tools/fakeVirtualSupport.js';

test('non-object initial data message', async t => {
const vom = makeFakeVirtualObjectManager();
// 'goodInit' is () => ({ value: 0 })
const goodInit = () => ({ value: 0 });

// 'badInit' is () => { value: 0 }
//
// badInit is subtly wrong: it returns undefined, and includes an
// unused "value:" label, plus a side-effect-free evaluation of a 0
// constant. A linter might catch this, but maybe you didn't run
// one.
//
// I frequently mean to write goodInit and write badInit
// instead. It's such an easy mistake to make, so the VOM's instance
// maker will catch it and complain.
//
// note: it is not easy to convince our editors and linters and
// prettiers to leave this erroneous code in place

// eslint-disable-next-line
/* prettier-ignore */ const badInit = () => { value: 0 };

const behavior = {};
const makeGoodThing = vom.defineKind('goodthing', goodInit, behavior);
const makeBadThing = vom.defineKind('badthing', badInit, behavior);
makeGoodThing();
const m = s => ({ message: s });
t.throws(() => makeBadThing(), m(/initial data must be object, not /));
});

0 comments on commit 0a1c34e

Please sign in to comment.