Hmbown/CodeWhale · error · TypeError

task(): expected an options object

Error message

task(): expected an options object

What it means

Thrown by the prelude's `globalThis.task` in vm.rs when the first argument is null or not an object. `task(opts)` requires an options object that is serialized to the host via `hostTask(JSON.stringify(opts))`; anything else (null, a string, a number, undefined) is rejected with this TypeError before the host call.

Solutions

  1. Pass an options object: `task({ kind: "...", ...params })`.
  2. If you intended to run several functions, use `parallel(thunks)` instead of task().
  3. Wrap primitives in the expected envelope shape before calling.
  4. Check the call site for a missing object literal (e.g. `task("fetch")` → `task({ kind: "fetch" })`).

Example fix

// before
const data = await task("fetch_user", userId);
// after
const data = await task({ kind: "fetch_user", userId });
Defensive patterns

Strategy: type-guard

Validate before calling

function assertTaskOpts(opts) {
  if (opts === null || typeof opts !== 'object') {
    throw new TypeError('task() requires an options object like { kind: "..." }');
  }
}

Type guard

function isTaskOpts(v) {
  return v !== null && typeof v === 'object' && !Array.isArray(v);
}

Try / catch

try {
  const result = await task(opts);
} catch (e) {
  if (e instanceof TypeError && e.message === 'task(): expected an options object') {
    console.error('wrap task parameters in an object: task({ kind, ...params })');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `task()` with no arguments, `task(null)`, `task("kind")`, or `task(42)`; also calling with a non-object like an array-of-thunks where an options object was expected.

Common situations: Confusing task() with parallel()'s thunk API; forgetting the options wrapper around the task kind; a refactor that changed the call signature.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/f4f55729c17ddba4. Report an issue: GitHub.

Appendix: source

Thrown at crates/workflow-js/src/vm.rs:1526

  // The failure ledger for one fan-out, attached to the resolved array as a
  // non-enumerable `errors` property. Non-enumerable and non-index, so the
  // array's contents, length, and JSON encoding are byte-identical to before:
  // `results.filter(Boolean)` still works, and a script that wants to know
  // WHY a slot is null can now ask instead of guessing.
  const attachSlotErrors = (results, errors) => {
    errors.sort((a, b) => a.index - b.index);
    Object.defineProperty(results, "errors", {
      value: Object.freeze(errors.map((entry) => Object.freeze(entry))),
      enumerable: false,
      configurable: false,
      writable: false,
    });
    return results;
  };

  globalThis.task = async (opts) => {
    if (opts === null || typeof opts !== "object") {
      throw new TypeError("task(): expected an options object");
    }
    const envelope = JSON.parse(await hostTask(JSON.stringify(opts)));
    if (envelope.error !== undefined) {
      const err = new Error(envelope.error);
      // The host always names the kind; a missing one means an envelope this
      // prelude did not produce, which is not a typed task failure.
      err.kind = HOST_KINDS.indexOf(envelope.error_kind) !== -1
        ? envelope.error_kind
        : SCRIPT_KIND;
      throw err;
    }
    return envelope.value;
  };

  globalThis.parallel = (thunks, opts) => {
    if (!Array.isArray(thunks)) {
      throw new TypeError("parallel(): expected an array of thunks");
    }

View on GitHub (pinned to 73e0f67d83)