UNPKG

1.52 kBJavaScriptView Raw
1import * as ErrorOverlay from "react-error-overlay"
2
3// Report runtime errors
4ErrorOverlay.startReportingRuntimeErrors({
5 onError: () => {},
6 filename: `/commons.js`,
7})
8ErrorOverlay.setEditorHandler(errorLocation =>
9 window.fetch(
10 `/__open-stack-frame-in-editor?fileName=` +
11 window.encodeURIComponent(errorLocation.fileName) +
12 `&lineNumber=` +
13 window.encodeURIComponent(errorLocation.lineNumber || 1)
14 )
15)
16
17const errorMap = {}
18
19function flat(arr) {
20 return Array.prototype.flat ? arr.flat() : [].concat(...arr)
21}
22
23const handleErrorOverlay = () => {
24 const errors = Object.values(errorMap)
25 let errorStringsToDisplay = []
26 if (errors.length > 0) {
27 errorStringsToDisplay = flat(errors)
28 .map(error => {
29 if (typeof error === `string`) {
30 return error
31 } else if (typeof error === `object`) {
32 const errorStrBuilder = [error.text]
33
34 if (error.filePath) {
35 errorStrBuilder.push(`File: ${error.filePath}`)
36 }
37
38 return errorStrBuilder.join(`\n\n`)
39 }
40
41 return null
42 })
43 .filter(Boolean)
44 }
45
46 if (errorStringsToDisplay.length > 0) {
47 ErrorOverlay.reportBuildError(errorStringsToDisplay.join(`\n\n`))
48 } else {
49 ErrorOverlay.dismissBuildError()
50 }
51}
52
53export const clearError = errorID => {
54 delete errorMap[errorID]
55 handleErrorOverlay()
56}
57
58export const reportError = (errorID, error) => {
59 if (error) {
60 errorMap[errorID] = error
61 }
62 handleErrorOverlay()
63}
64
65export { errorMap }