Skip to content

Commit 8183012

Browse files
authored
fix(gatsyby): improve error messages for errors outside of react" (#30031)
1 parent bce0b21 commit 8183012

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

packages/gatsby/cache-dir/__tests__/ensure-resources.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { render, getNodeText, cleanup } from "@testing-library/react"
44

55
jest.mock(`../loader`, () => {
66
return {
7+
PageResourceStatus: {
8+
Error: `error`,
9+
},
710
loadPageSync(path: string): { loadPageSync: boolean; path: string } {
811
return { loadPageSync: true, path }
912
},

packages/gatsby/cache-dir/ensure-resources.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ class EnsureResources extends React.Component {
88
const { location, pageResources } = props
99
this.state = {
1010
location: { ...location },
11-
pageResources: pageResources || loader.loadPageSync(location.pathname),
11+
pageResources:
12+
pageResources ||
13+
loader.loadPageSync(location.pathname, { withErrorDetails: true }),
1214
}
1315
}
1416

1517
static getDerivedStateFromProps({ location }, prevState) {
1618
if (prevState.location.href !== location.href) {
17-
const pageResources = loader.loadPageSync(location.pathname)
19+
const pageResources = loader.loadPageSync(location.pathname, {
20+
withErrorDetails: true,
21+
})
22+
1823
return {
1924
pageResources,
2025
location: { ...location },
@@ -82,12 +87,20 @@ class EnsureResources extends React.Component {
8287
}
8388

8489
render() {
85-
if (process.env.NODE_ENV !== `production` && !this.state.pageResources) {
86-
throw new Error(
87-
`EnsureResources was not able to find resources for path: "${this.props.location.pathname}"
90+
if (
91+
process.env.NODE_ENV !== `production` &&
92+
(!this.state.pageResources ||
93+
this.state.pageResources.status === PageResourceStatus.Error)
94+
) {
95+
const message = `EnsureResources was not able to find resources for path: "${this.props.location.pathname}"
8896
This typically means that an issue occurred building components for that path.
8997
Run \`gatsby clean\` to remove any cached elements.`
90-
)
98+
if (this.state.pageResources?.error) {
99+
console.error(message)
100+
throw this.state.pageResources.error
101+
}
102+
103+
throw new Error(message)
91104
}
92105

93106
return this.props.children(this.state)

packages/gatsby/cache-dir/loader.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,22 @@ export class BaseLoader {
348348
return inFlightPromise
349349
}
350350

351-
// returns undefined if loading page ran into errors
352-
loadPageSync(rawPath) {
351+
// returns undefined if the page does not exists in cache
352+
loadPageSync(rawPath, options = {}) {
353353
const pagePath = findPath(rawPath)
354354
if (this.pageDb.has(pagePath)) {
355-
const pageData = this.pageDb.get(pagePath).payload
356-
return pageData
355+
const pageData = this.pageDb.get(pagePath)
356+
357+
if (pageData.payload) {
358+
return pageData.payload
359+
}
360+
361+
if (options?.withErrorDetails) {
362+
return {
363+
error: pageData.error,
364+
status: pageData.status,
365+
}
366+
}
357367
}
358368
return undefined
359369
}
@@ -546,7 +556,9 @@ export const publicLoader = {
546556
getResourceURLsForPathname: rawPath =>
547557
instance.getResourceURLsForPathname(rawPath),
548558
loadPage: rawPath => instance.loadPage(rawPath),
549-
loadPageSync: rawPath => instance.loadPageSync(rawPath),
559+
// TODO add deprecation to v4 so people use withErrorDetails and then we can remove in v5 and change default behaviour
560+
loadPageSync: (rawPath, options = {}) =>
561+
instance.loadPageSync(rawPath, options),
550562
prefetch: rawPath => instance.prefetch(rawPath),
551563
isPageNotFound: rawPath => instance.isPageNotFound(rawPath),
552564
hovering: rawPath => instance.hovering(rawPath),

0 commit comments

Comments
 (0)