`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!
So...this issue is closed?
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!