Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make error box messages friendlier #2123

Merged
merged 1 commit into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is reversed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh. It is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2b59654

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