Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] feat(interpreter): allow to provide errorListeners #2841

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,18 @@ export class Interpreter<
// Send "error.platform.id" to this (parent).
this.send(toSCXMLEvent(errorEvent as any, { origin: id }));
} catch (error) {
reportUnhandledExceptionOnInvocation(errorData, error, id);
if (this.parent) {
this.parent.send(
{
type: 'xstate.error',
data: error
} as EventObject,
undefined,
true
);
} else {
reportUnhandledExceptionOnInvocation(errorData, error, id);
}
if (this.devTools) {
this.devTools.send(errorEvent, this.state, true);
}
Expand Down
55 changes: 55 additions & 0 deletions packages/core/test/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,61 @@ Event: {\\"type\\":\\"SOME_EVENT\\"}"
);
});

it('should be handle child errors with errorListener', (done) => {
roggervalf marked this conversation as resolved.
Show resolved Hide resolved
const failureMachine = Machine<typeof context>(
{
id: 'failure',
context,
initial: 'active',
states: {
active: {
after: {
100: {
target: 'failure'
}
}
},
failure: {
invoke: {
src: 'failure'
}
}
}
},
{
services: {
failure: async () => {
throw new Error('error');
}
}
}
);

const parentMachine = Machine({
initial: 'foo',
states: {
foo: {
invoke: {
id: 'child',
src: failureMachine
}
}
}
});

const intervalService = interpret(parentMachine).start();

expect(isObservable(intervalService)).toBeTruthy();

intervalService.subscribe(
() => {},
(error) => {
expect(error.data).toBeInstanceOf(Error);
done();
}
);
});

it('should be interoperable with RxJS, etc. via Symbol.observable', (done) => {
let count = 0;
const intervalService = interpret(intervalMachine).start();
Expand Down