-
-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathhandleError.js
71 lines (53 loc) · 1.98 KB
/
handleError.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from 'react';
import ReactDOMServer from 'react-dom/server';
function handleGeneratorFunctionIssue(options) {
const { e, name } = options;
let msg = '';
if (name) {
const lastLine =
'A generator function takes a single arg of props (and the location for react-router) ' +
'and returns a ReactElement.';
let shouldBeGeneratorError =
`ERROR: ReactOnRails is incorrectly detecting generator function to be false. The React
component \'${name}\' seems to be a generator function.\n${lastLine}`;
const reMatchShouldBeGeneratorError = /Can't add property context, object is not extensible/;
if (reMatchShouldBeGeneratorError.test(e.message)) {
msg += `${shouldBeGeneratorError}\n\n`;
console.error(shouldBeGeneratorError);
}
shouldBeGeneratorError =
`ERROR: ReactOnRails is incorrectly detecting generatorFunction to be true, but the React
component \'${name}\' is not a generator function.\n${lastLine}`;
const reMatchShouldNotBeGeneratorError = /Cannot call a class as a function/;
if (reMatchShouldNotBeGeneratorError.test(e.message)) {
msg += `${shouldBeGeneratorError}\n\n`;
console.error(shouldBeGeneratorError);
}
}
return msg;
}
export default (options) => {
const { e, jsCode, serverSide } = options;
console.error('Exception in rendering!');
let msg = handleGeneratorFunctionIssue(options);
if (jsCode) {
console.error(`JS code was: ${jsCode}`);
}
if (e.fileName) {
console.error(`location: ${e.fileName}:${e.lineNumber}`);
}
console.error(`message: ${e.message}`);
console.error(`stack: ${e.stack}`);
msg += `Exception in rendering!
${e.fileName ? `\nlocation: ${e.fileName}:${e.lineNumber}` : ''}
Message: ${e.message}
${e.stack}`;
if (serverSide) {
const reactElement = React.createElement('pre', null, msg);
return ReactDOMServer.renderToString(reactElement);
} else {
var newError = new Error(msg);
newError.cause = e;
throw newError;
}
};