Skip to content

Commit b84ea14

Browse files
authored
feat(gatsby): Add top-level error handling to state machine (#25995)
1 parent 59de77d commit b84ea14

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/gatsby/src/state-machines/develop/actions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ export const finishParentSpan = ({ parentSpan }: IBuildContext): void =>
129129

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

132+
export const logError: ActionFunction<IBuildContext, AnyEventObject> = (
133+
_context,
134+
event
135+
) => {
136+
reporter.error(event.data)
137+
}
138+
139+
export const panic: ActionFunction<IBuildContext, AnyEventObject> = (
140+
_context,
141+
event
142+
) => {
143+
reporter.panic(event.data)
144+
}
145+
132146
/**
133147
* Event handler used in all states where we're not ready to process a file change
134148
* Instead we add it to a batch to process when we're next idle
@@ -158,4 +172,6 @@ export const buildActions: ActionFunctionMap<IBuildContext, AnyEventObject> = {
158172
markSourceFilesClean,
159173
saveDbState,
160174
setQueryRunningFinished,
175+
panic,
176+
logError,
161177
}

packages/gatsby/src/state-machines/develop/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
4545
target: `initializingData`,
4646
actions: [`assignStoreAndWorkerPool`, `spawnMutationListener`],
4747
},
48+
onError: {
49+
actions: `panic`,
50+
},
4851
},
4952
},
5053
// Sourcing nodes, customising and inferring schema, then running createPages
@@ -77,6 +80,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
7780
],
7881
target: `runningPostBootstrap`,
7982
},
83+
onError: {
84+
actions: `logError`,
85+
target: `waiting`,
86+
},
8087
},
8188
},
8289
runningPostBootstrap: {
@@ -132,6 +139,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
132139
target: `waiting`,
133140
},
134141
],
142+
onError: {
143+
actions: `logError`,
144+
target: `waiting`,
145+
},
135146
},
136147
},
137148
// Recompile the JS bundle
@@ -142,6 +153,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
142153
actions: `markSourceFilesClean`,
143154
target: `waiting`,
144155
},
156+
onError: {
157+
actions: `logError`,
158+
target: `waiting`,
159+
},
145160
},
146161
},
147162
// Spin up webpack and socket.io
@@ -156,6 +171,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
156171
`markSourceFilesClean`,
157172
],
158173
},
174+
onError: {
175+
actions: `panic`,
176+
target: `waiting`,
177+
},
159178
},
160179
},
161180
// Idle, waiting for events that make us rebuild
@@ -189,6 +208,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
189208
actions: `assignServiceResult`,
190209
target: `recreatingPages`,
191210
},
211+
onError: {
212+
actions: `panic`,
213+
target: `waiting`,
214+
},
192215
},
193216
},
194217
// Almost the same as initializing data, but skips various first-run stuff
@@ -223,6 +246,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
223246
],
224247
target: `runningQueries`,
225248
},
249+
onError: {
250+
actions: `logError`,
251+
target: `waiting`,
252+
},
226253
},
227254
},
228255
// Rebuild pages if a node has been mutated outside of sourceNodes
@@ -236,6 +263,10 @@ const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
236263
actions: `assignServiceResult`,
237264
target: `runningQueries`,
238265
},
266+
onError: {
267+
actions: `logError`,
268+
target: `waiting`,
269+
},
239270
},
240271
},
241272
},

0 commit comments

Comments
 (0)