Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show GraphQL compile errors in browser overlay (#6247)
This approach to displaying GraphQL compilation errors in the browser console utilizes the WebSocket connecting node running gatsby package responsible for executing GraphQL queries and the cache loaded and used on the browser side (thanks @pieh for helping in this). #5234
- Loading branch information
Showing
9 changed files
with
162 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,5 @@ | ||
{ | ||
babelrc: false, | ||
presets: [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
loose: true, | ||
modules: false, | ||
useBuiltIns: "usage", | ||
shippedProposals: true, | ||
targets: { | ||
browsers: [">0.25%", "not dead"], | ||
}, | ||
}, | ||
], | ||
[ | ||
"@babel/preset-react", | ||
{ | ||
useBuiltIns: true, | ||
pragma: "React.createElement", | ||
}, | ||
], | ||
], | ||
plugins: [ | ||
[ | ||
"@babel/plugin-proposal-class-properties", | ||
{ | ||
loose: true, | ||
}, | ||
], | ||
"@babel/plugin-syntax-dynamic-import", | ||
[ | ||
"@babel/plugin-transform-runtime", | ||
{ | ||
helpers: true, | ||
regenerator: true, | ||
corejs: false, | ||
}, | ||
], | ||
], | ||
"presets": [ | ||
["babel-preset-gatsby"] | ||
] | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/gatsby/cache-dir/__tests__/error-overlay-handler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import "@babel/polyfill" | ||
const { | ||
reportError, | ||
clearError, | ||
errorMap, | ||
} = require(`../error-overlay-handler`) | ||
|
||
import * as ErrorOverlay from "react-error-overlay" | ||
|
||
jest.mock(`react-error-overlay`, () => { | ||
return { | ||
reportBuildError: jest.fn(), | ||
dismissBuildError: jest.fn(), | ||
startReportingRuntimeErrors: jest.fn(), | ||
setEditorHandler: jest.fn(), | ||
} | ||
}) | ||
|
||
beforeEach(() => { | ||
ErrorOverlay.reportBuildError.mockClear() | ||
ErrorOverlay.dismissBuildError.mockClear() | ||
}) | ||
|
||
describe(`errorOverlayHandler`, () => { | ||
describe(`clearError()`, () => { | ||
beforeEach(() => { | ||
reportError(`foo`, `error`) | ||
reportError(`bar`, `error`) | ||
}) | ||
afterAll(() => { | ||
clearError(`foo`) | ||
clearError(`bar`) | ||
}) | ||
it(`should clear specific error type`, () => { | ||
expect(Object.keys(errorMap)).toHaveLength(2) | ||
clearError(`foo`) | ||
expect(Object.keys(errorMap)).toHaveLength(1) | ||
expect(ErrorOverlay.dismissBuildError).not.toHaveBeenCalled() | ||
}) | ||
|
||
it(`should call ErrorOverlay to dismiss build errors`, () => { | ||
clearError(`foo`) | ||
clearError(`bar`) | ||
expect(ErrorOverlay.dismissBuildError).toHaveBeenCalled() | ||
}) | ||
}) | ||
describe(`reportErrorOverlay()`, () => { | ||
it(`should not add error if it's empty and not call ErrorOverlay`, () => { | ||
reportError(`foo`, null) | ||
expect(Object.keys(errorMap)).toHaveLength(0) | ||
expect(ErrorOverlay.reportBuildError).not.toHaveBeenCalled() | ||
}) | ||
it(`should add error if it has a truthy value and call ErrorOverlay`, () => { | ||
reportError(`foo`, `bar`) | ||
expect(Object.keys(errorMap)).toHaveLength(1) | ||
expect(ErrorOverlay.reportBuildError).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as ErrorOverlay from "react-error-overlay" | ||
|
||
// Report runtime errors | ||
ErrorOverlay.startReportingRuntimeErrors({ | ||
onError: () => {}, | ||
filename: `/commons.js`, | ||
}) | ||
ErrorOverlay.setEditorHandler(errorLocation => | ||
window.fetch( | ||
`/__open-stack-frame-in-editor?fileName=` + | ||
window.encodeURIComponent(errorLocation.fileName) + | ||
`&lineNumber=` + | ||
window.encodeURIComponent(errorLocation.lineNumber || 1) | ||
) | ||
) | ||
|
||
const errorMap = {} | ||
|
||
const handleErrorOverlay = () => { | ||
const errors = Object.values(errorMap) | ||
if (errors.length > 0) { | ||
const errorMsg = errors.join(`\n\n`) | ||
ErrorOverlay.reportBuildError(errorMsg) | ||
} else { | ||
ErrorOverlay.dismissBuildError() | ||
} | ||
} | ||
|
||
export const clearError = errorID => { | ||
delete errorMap[errorID] | ||
handleErrorOverlay() | ||
} | ||
|
||
export const reportError = (errorID, error) => { | ||
if (error) { | ||
errorMap[errorID] = error | ||
} | ||
handleErrorOverlay() | ||
} | ||
|
||
export { errorMap } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters