From c0a2a55a0ef3a95ab3ae122ac485873af232b553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rogger=20andr=C3=A9=20valverde=20flores?= Date: Sun, 28 Nov 2021 15:49:40 -0500 Subject: [PATCH] refactor(interpreter): handle more error cases --- packages/core/src/interpreter.ts | 43 +++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/core/src/interpreter.ts b/packages/core/src/interpreter.ts index ad2c915f5c..dabc1e878e 100644 --- a/packages/core/src/interpreter.ts +++ b/packages/core/src/interpreter.ts @@ -315,9 +315,11 @@ export class Interpreter< } } - private sendError(error: Error): void { - for (const listener of this.errorListeners) { - listener(doneInvoke(this.id, error)); + private sendError(errorEvent: Event | SCXML.Event): void { + if (this.errorListeners.size) { + for (const listener of this.errorListeners) { + listener(errorEvent as EventObject); + } } } @@ -581,13 +583,18 @@ export class Interpreter< */ public send = ( event: SingleOrArray> | SCXML.Event, - payload?: EventData + payload?: EventData, + sendError = false ): State => { if (isArray(event)) { this.batch(event); return this.state; } + if (sendError) { + this.sendError(event as any); + } + const _event = toSCXMLEvent(toEventObject(event as Event, payload)); if (this.status === InterpreterStatus.Stopped) { @@ -817,10 +824,14 @@ export class Interpreter< }); } catch (err) { if (this.parent) { - this.parent.send({ - type: 'xstate.error', - data: err - } as EventObject); + this.parent.send( + { + type: 'xstate.error', + data: err + } as EventObject, + undefined, + true + ); } throw err; @@ -1084,11 +1095,11 @@ export class Interpreter< } catch (error) { reportUnhandledExceptionOnInvocation(errorData, error, id); if (this.devTools) { - this.devTools.send(errorEvent, this.state); - } - if (this.errorListeners.size) { - this.sendError(error); + this.devTools.send(errorEvent, this.state, true); } + + this.sendError(errorEvent); + if (this.machine.strict) { // it would be better to always stop the state machine if unhandled // exception/promise rejection happens but because we don't want to @@ -1166,7 +1177,7 @@ export class Interpreter< receivers.add(newListener); }); } catch (err) { - this.send(error(id, err) as any); + this.send(error(id, err) as any, undefined, true); } if (isPromiseLike(callbackStop)) { @@ -1216,7 +1227,11 @@ export class Interpreter< }, (err) => { this.removeChild(id); - this.send(toSCXMLEvent(error(id, err) as any, { origin: id })); + this.send( + toSCXMLEvent(error(id, err) as any, { origin: id }), + undefined, + true + ); }, () => { this.removeChild(id);