Commit 3521eb7c authored by Dan Abramov's avatar Dan Abramov Committed by GitHub
Browse files

Make error box messages friendlier (#2123)

parent a4bd567d
Showing with 23 additions and 6 deletions
+23 -6
...@@ -127,8 +127,8 @@ function createFrame( ...@@ -127,8 +127,8 @@ function createFrame(
lastElement: boolean lastElement: boolean
) { ) {
const { compiled } = frameSetting; const { compiled } = frameSetting;
let { functionName } = frame;
const { const {
functionName,
fileName, fileName,
lineNumber, lineNumber,
columnNumber, columnNumber,
...@@ -139,6 +139,14 @@ function createFrame( ...@@ -139,6 +139,14 @@ function createFrame(
_originalScriptCode: sourceLines, _originalScriptCode: sourceLines,
} = frame; } = frame;
// TODO: find a better place for this.
// Chrome has a bug with inferring function.name:
// https://github.com/facebookincubator/create-react-app/issues/2097
// Let's ignore a meaningless name we get for top-level modules.
if (functionName === 'Object.friendlySyntaxErrorLabel') {
functionName = '(anonymous function)';
}
let url; let url;
if (!compiled && sourceFileName && sourceLineNumber) { if (!compiled && sourceFileName && sourceLineNumber) {
url = sourceFileName + ':' + sourceLineNumber; url = sourceFileName + ':' + sourceLineNumber;
......
...@@ -50,11 +50,19 @@ function createOverlay( ...@@ -50,11 +50,19 @@ function createOverlay(
// Create header // Create header
const header = document.createElement('div'); const header = document.createElement('div');
applyStyles(header, headerStyle); applyStyles(header, headerStyle);
if (message.match(/^\w*:/)) {
header.appendChild(document.createTextNode(message)); // Make message prettier
} else { let finalMessage = message.match(/^\w*:/) ? name + ': ' + message : message;
header.appendChild(document.createTextNode(name + ': ' + message)); finalMessage = finalMessage
} // TODO: maybe remove this prefix from fbjs?
// It's just scaring people
.replace('Invariant Violation: ', '')
// Break the actionable part to the next line.
// AFAIK React 16+ should already do this.
.replace(' Check the render method', '\n\nCheck the render method');
// Put it in the DOM
header.appendChild(document.createTextNode(finalMessage));
container.appendChild(header); container.appendChild(header);
// Create trace // Create trace
......
...@@ -65,6 +65,7 @@ const headerStyle = { ...@@ -65,6 +65,7 @@ const headerStyle = {
'font-size': '1.7em', 'font-size': '1.7em',
'font-weight': 'bold', 'font-weight': 'bold',
color: red, color: red,
'white-space': 'pre-wrap',
}; };
const functionNameStyle = { const functionNameStyle = {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment