From b84ea14584b6b1f877387f878787fb19578fbdda Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 28 Jul 2020 09:25:32 +0100 Subject: [PATCH] feat(gatsby): Add top-level error handling to state machine (#25995) --- .../src/state-machines/develop/actions.ts | 16 ++++++++++ .../src/state-machines/develop/index.ts | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/gatsby/src/state-machines/develop/actions.ts b/packages/gatsby/src/state-machines/develop/actions.ts index 56fc89edb4ae3..fd278117881e5 100644 --- a/packages/gatsby/src/state-machines/develop/actions.ts +++ b/packages/gatsby/src/state-machines/develop/actions.ts @@ -129,6 +129,20 @@ export const finishParentSpan = ({ parentSpan }: IBuildContext): void => export const saveDbState = (): Promise => saveState() +export const logError: ActionFunction = ( + _context, + event +) => { + reporter.error(event.data) +} + +export const panic: ActionFunction = ( + _context, + event +) => { + reporter.panic(event.data) +} + /** * Event handler used in all states where we're not ready to process a file change * Instead we add it to a batch to process when we're next idle @@ -158,4 +172,6 @@ export const buildActions: ActionFunctionMap = { markSourceFilesClean, saveDbState, setQueryRunningFinished, + panic, + logError, } diff --git a/packages/gatsby/src/state-machines/develop/index.ts b/packages/gatsby/src/state-machines/develop/index.ts index 82dbe6d7f1de5..b5e8371d52c4d 100644 --- a/packages/gatsby/src/state-machines/develop/index.ts +++ b/packages/gatsby/src/state-machines/develop/index.ts @@ -45,6 +45,9 @@ const developConfig: MachineConfig = { target: `initializingData`, actions: [`assignStoreAndWorkerPool`, `spawnMutationListener`], }, + onError: { + actions: `panic`, + }, }, }, // Sourcing nodes, customising and inferring schema, then running createPages @@ -77,6 +80,10 @@ const developConfig: MachineConfig = { ], target: `runningPostBootstrap`, }, + onError: { + actions: `logError`, + target: `waiting`, + }, }, }, runningPostBootstrap: { @@ -132,6 +139,10 @@ const developConfig: MachineConfig = { target: `waiting`, }, ], + onError: { + actions: `logError`, + target: `waiting`, + }, }, }, // Recompile the JS bundle @@ -142,6 +153,10 @@ const developConfig: MachineConfig = { actions: `markSourceFilesClean`, target: `waiting`, }, + onError: { + actions: `logError`, + target: `waiting`, + }, }, }, // Spin up webpack and socket.io @@ -156,6 +171,10 @@ const developConfig: MachineConfig = { `markSourceFilesClean`, ], }, + onError: { + actions: `panic`, + target: `waiting`, + }, }, }, // Idle, waiting for events that make us rebuild @@ -189,6 +208,10 @@ const developConfig: MachineConfig = { actions: `assignServiceResult`, target: `recreatingPages`, }, + onError: { + actions: `panic`, + target: `waiting`, + }, }, }, // Almost the same as initializing data, but skips various first-run stuff @@ -223,6 +246,10 @@ const developConfig: MachineConfig = { ], target: `runningQueries`, }, + onError: { + actions: `logError`, + target: `waiting`, + }, }, }, // Rebuild pages if a node has been mutated outside of sourceNodes @@ -236,6 +263,10 @@ const developConfig: MachineConfig = { actions: `assignServiceResult`, target: `runningQueries`, }, + onError: { + actions: `logError`, + target: `waiting`, + }, }, }, },