Skip to content

Commit

Permalink
feat(gatsby): Add top-level error handling to state machine (#25995)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Jul 28, 2020
1 parent 59de77d commit b84ea14
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/gatsby/src/state-machines/develop/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ export const finishParentSpan = ({ parentSpan }: IBuildContext): void =>

export const saveDbState = (): Promise<void> => saveState()

export const logError: ActionFunction<IBuildContext, AnyEventObject> = (
_context,
event
) => {
reporter.error(event.data)
}

export const panic: ActionFunction<IBuildContext, AnyEventObject> = (
_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
Expand Down Expand Up @@ -158,4 +172,6 @@ export const buildActions: ActionFunctionMap<IBuildContext, AnyEventObject> = {
markSourceFilesClean,
saveDbState,
setQueryRunningFinished,
panic,
logError,
}
31 changes: 31 additions & 0 deletions packages/gatsby/src/state-machines/develop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
target: `initializingData`,
actions: [`assignStoreAndWorkerPool`, `spawnMutationListener`],
},
onError: {
actions: `panic`,
},
},
},
// Sourcing nodes, customising and inferring schema, then running createPages
Expand Down Expand Up @@ -77,6 +80,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
],
target: `runningPostBootstrap`,
},
onError: {
actions: `logError`,
target: `waiting`,
},
},
},
runningPostBootstrap: {
Expand Down Expand Up @@ -132,6 +139,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
target: `waiting`,
},
],
onError: {
actions: `logError`,
target: `waiting`,
},
},
},
// Recompile the JS bundle
Expand All @@ -142,6 +153,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
actions: `markSourceFilesClean`,
target: `waiting`,
},
onError: {
actions: `logError`,
target: `waiting`,
},
},
},
// Spin up webpack and socket.io
Expand All @@ -156,6 +171,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
`markSourceFilesClean`,
],
},
onError: {
actions: `panic`,
target: `waiting`,
},
},
},
// Idle, waiting for events that make us rebuild
Expand Down Expand Up @@ -189,6 +208,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
actions: `assignServiceResult`,
target: `recreatingPages`,
},
onError: {
actions: `panic`,
target: `waiting`,
},
},
},
// Almost the same as initializing data, but skips various first-run stuff
Expand Down Expand Up @@ -223,6 +246,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
],
target: `runningQueries`,
},
onError: {
actions: `logError`,
target: `waiting`,
},
},
},
// Rebuild pages if a node has been mutated outside of sourceNodes
Expand All @@ -236,6 +263,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
actions: `assignServiceResult`,
target: `runningQueries`,
},
onError: {
actions: `logError`,
target: `waiting`,
},
},
},
},
Expand Down

0 comments on commit b84ea14

Please sign in to comment.