matthewp / robot

🤖 A functional, immutable Finite State Machine library

Home Page:https://thisrobot.life

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`invoke` shouldn't swallow errors

cletusw opened this issue · comments

Currently, invoke swallows errors from the called function. This makes debugging issues harder and allows developers to neglect to provide error handlers. Instead it should rethrow if a handler was not provided (forcing the developer to provide one) or at the very least console.error.

Repro: https://jsbin.com/vowukapihi/2/edit?html,console,output

import { createMachine, interpret, invoke, reduce, state, transition } from 'https://jspm.dev/robot3@0.2.18';

async function throwsAnError() {
  await new Promise((resolve) => setTimeout(resolve, 1000));
  throw new Error('hi');
}

const machine = createMachine({
  asleep: invoke(throwsAnError,
    transition('done', 'breakfast'),
    // Uncomment to see error
    // transition('error', 'breakfast', reduce((ctx, ev) => { console.error(ev.error); }))
  ),
  breakfast: state()
});

const service = interpret(machine, () => {});

Hey @cletusw, indeed you should always be providing an error transition for invoke. In order to keep Robot light we don't do that kind of check. However the debug module is intended to be used in dev for these sorts of checks. Currently it doesn't check invoke, that's a good one to add!

Hi @cletusw and @matthewp, it seems that I managed to fix this behavior, could you approve?

Also I have improved the debugging a bit at #160 could you take a look too?

So...this issue is closed?

@flq #160 warns you if you don't provide an error handler. Have you seen otherwise?

Can confirm that the debug module errors with "When using invoke [current state: asleep] with Promise-returning function, you need to add 'error' state. Otherwise, robot will hide errors in Promise-returning function":

https://jsbin.com/fabiwehexo/edit?html,console,output

Thanks!