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 24, 2023
1 parent 760e0e2 commit ef539c1
Show file tree
Hide file tree
Showing 2 changed files with 26 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,22 @@
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();
// I frequently try to write goodInit and write badInit by
// mistake. It returns undefined, and includes an unused "value:"
// label and a side-effect-free evaluation of a 0 constant. A linter
// might catch this, but it's such an easy mistake to make that the
// VOM's instance maker will catch it and complain.
const goodInit = () => ({ value: 0 });
// eslint-disable-next-line
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 ef539c1

Please sign in to comment.