// The error model: an execution error aborts the macrostep. import { adv, ev, fail, raise, rec } from './util.mjs'; export default [ { name: 'guard-error-no-onerror', description: 'A guard that throws, with no onError on any active state: the actor takes the error status and stops handling events.', ref: 'errors.test.ts', machine: { id: 'm', initial: 'a', states: { a: { entry: [raise('LATER', { delay: 50, id: 'later' })], on: { BOOM: { guard: fail('guard failed'), target: 'b' }, OK: { target: 'b' } } }, b: {} } }, script: [ev('BOOM'), ev('OK'), adv(100)] }, { name: 'guard-error-onerror-recovers', description: 'A guard throws while an active state declares onError: the macrostep reruns from the pre-event snapshot with xstate.error.execution.', ref: 'errors.test.ts', machine: { id: 'm', initial: 'p', states: { p: { initial: 'a', onError: { target: 'failed', actions: [rec('recovered')] }, states: { a: { on: { BOOM: { guard: fail('guard failed'), target: 'b' }, OK: { target: 'b' } } }, b: {} } }, failed: { on: { RETRY: { target: 'p' } } } } }, script: [ev('BOOM'), ev('RETRY'), ev('OK'), ev('BOOM')] }, { name: 'error-aborts-whole-macrostep', description: 'An error in a later microstep (an always guard) discards the earlier microsteps\' state, context and effects; recovery starts from the pre-event snapshot.', machine: { id: 'm', initial: 'idle', context: { touched: false }, states: { idle: { onError: { target: 'recovering', actions: [rec('onError')] }, on: { GO: { target: 'step1', context: { touched: true }, actions: [rec('go'), raise('NEXT')] } } }, step1: { entry: [rec('enter step1'), raise('TIMER', { delay: 100, id: 't' })], on: { NEXT: { target: 'step2' } } }, step2: { always: { guard: fail('always failed'), target: 'idle' } }, recovering: {} } }, script: [ev('GO'), adv(200)] }, { name: 'onerror-without-match', description: 'onError exists on an inactive state only, so the error is not recoverable even though the machine declares onError.', machine: { id: 'm', initial: 'a', states: { a: { on: { BOOM: { guard: fail('nope'), target: 'b' } } }, b: { onError: { target: 'a' } } } }, script: [ev('BOOM'), ev('BOOM')] }, { name: 'error-in-recovery', description: 'The onError transition itself fails (its guard throws), so the actor takes the error status.', machine: { id: 'm', initial: 'a', states: { a: { onError: { guard: fail('again'), target: 'b' }, on: { BOOM: { guard: fail('first'), target: 'b' } } }, b: {} } }, script: [ev('BOOM'), ev('BOOM')] }, { name: 'error-in-timer-macrostep', description: 'An error while processing a timer event (after transition guard) recovers through onError.', machine: { id: 'm', initial: 'a', onError: { target: '.recovered' }, states: { a: { after: { 10: { guard: fail('timer guard'), target: 'b' } } }, b: {}, recovered: {} } }, script: [adv(10), adv(10)] }, { name: 'error-in-delayed-raise-targetless-recovery', description: 'An error while handling an event delivered by a delayed raise, recovered by a targetless onError: the recovery reruns from the snapshot before the timer fired.', machine: { id: 'm', initial: 'a', states: { a: { onError: { actions: [rec('recovered')] }, entry: [raise('TICK', { delay: 10, id: 'tick' })], on: { TICK: { guard: fail('tick guard'), target: 'b' }, AGAIN: { target: 'a', reenter: true } } }, b: {} } }, script: [adv(10), adv(10), ev('AGAIN'), adv(10)] } ];