Skip to content

Commit

Permalink
Make error box messages friendlier (#2123)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed May 11, 2017
1 parent a4bd567 commit 3521eb7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 9 additions & 1 deletion packages/react-error-overlay/src/components/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ function createFrame(
lastElement: boolean
) {
const { compiled } = frameSetting;
let { functionName } = frame;
const {
functionName,
fileName,
lineNumber,
columnNumber,
Expand All @@ -139,6 +139,14 @@ function createFrame(
_originalScriptCode: sourceLines,
} = 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;
if (!compiled && sourceFileName && sourceLineNumber) {
url = sourceFileName + ':' + sourceLineNumber;
Expand Down
18 changes: 13 additions & 5 deletions packages/react-error-overlay/src/components/overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ function createOverlay(
// Create header
const header = document.createElement('div');
applyStyles(header, headerStyle);
if (message.match(/^\w*:/)) {
header.appendChild(document.createTextNode(message));
} else {
header.appendChild(document.createTextNode(name + ': ' + message));
}

// Make message prettier
let finalMessage = message.match(/^\w*:/) ? name + ': ' + message : 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);

// Create trace
Expand Down
1 change: 1 addition & 0 deletions packages/react-error-overlay/src/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const headerStyle = {
'font-size': '1.7em',
'font-weight': 'bold',
color: red,
'white-space': 'pre-wrap',
};

const functionNameStyle = {
Expand Down

0 comments on commit 3521eb7

Please sign in to comment.