Skip to content

Commit c0a2a55

Browse files
committed
refactor(interpreter): handle more error cases
1 parent f5027b0 commit c0a2a55

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

packages/core/src/interpreter.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,11 @@ export class Interpreter<
315315
}
316316
}
317317

318-
private sendError(error: Error): void {
319-
for (const listener of this.errorListeners) {
320-
listener(doneInvoke(this.id, error));
318+
private sendError(errorEvent: Event<TEvent> | SCXML.Event<TEvent>): void {
319+
if (this.errorListeners.size) {
320+
for (const listener of this.errorListeners) {
321+
listener(errorEvent as EventObject);
322+
}
321323
}
322324
}
323325

@@ -581,13 +583,18 @@ export class Interpreter<
581583
*/
582584
public send = (
583585
event: SingleOrArray<Event<TEvent>> | SCXML.Event<TEvent>,
584-
payload?: EventData
586+
payload?: EventData,
587+
sendError = false
585588
): State<TContext, TEvent, TStateSchema, TTypestate> => {
586589
if (isArray(event)) {
587590
this.batch(event);
588591
return this.state;
589592
}
590593

594+
if (sendError) {
595+
this.sendError(event as any);
596+
}
597+
591598
const _event = toSCXMLEvent(toEventObject(event as Event<TEvent>, payload));
592599

593600
if (this.status === InterpreterStatus.Stopped) {
@@ -817,10 +824,14 @@ export class Interpreter<
817824
});
818825
} catch (err) {
819826
if (this.parent) {
820-
this.parent.send({
821-
type: 'xstate.error',
822-
data: err
823-
} as EventObject);
827+
this.parent.send(
828+
{
829+
type: 'xstate.error',
830+
data: err
831+
} as EventObject,
832+
undefined,
833+
true
834+
);
824835
}
825836

826837
throw err;
@@ -1084,11 +1095,11 @@ export class Interpreter<
10841095
} catch (error) {
10851096
reportUnhandledExceptionOnInvocation(errorData, error, id);
10861097
if (this.devTools) {
1087-
this.devTools.send(errorEvent, this.state);
1088-
}
1089-
if (this.errorListeners.size) {
1090-
this.sendError(error);
1098+
this.devTools.send(errorEvent, this.state, true);
10911099
}
1100+
1101+
this.sendError(errorEvent);
1102+
10921103
if (this.machine.strict) {
10931104
// it would be better to always stop the state machine if unhandled
10941105
// exception/promise rejection happens but because we don't want to
@@ -1166,7 +1177,7 @@ export class Interpreter<
11661177
receivers.add(newListener);
11671178
});
11681179
} catch (err) {
1169-
this.send(error(id, err) as any);
1180+
this.send(error(id, err) as any, undefined, true);
11701181
}
11711182

11721183
if (isPromiseLike(callbackStop)) {
@@ -1216,7 +1227,11 @@ export class Interpreter<
12161227
},
12171228
(err) => {
12181229
this.removeChild(id);
1219-
this.send(toSCXMLEvent(error(id, err) as any, { origin: id }));
1230+
this.send(
1231+
toSCXMLEvent(error(id, err) as any, { origin: id }),
1232+
undefined,
1233+
true
1234+
);
12201235
},
12211236
() => {
12221237
this.removeChild(id);

0 commit comments

Comments
 (0)