From 738de8cfa846936ae754ccfda607e4fb41c2170b Mon Sep 17 00:00:00 2001 From: CommitSyncScript Date: Fri, 28 Jun 2013 16:33:45 -0700 Subject: [PATCH] Improve `findComponentRoot` Error Message Instead of simply logging the React ID of the `ancestorNode` when `findComponentRoot` fails, use a `try ... finally` to `console.error` the `ancestorNode`. This allows modern web inspectors to actually log a reference to the node (which may not have a React ID). This means when people run into the problem, they will not have to execute: require('ReactID').getNode(); NOTE: Admittedly, this will not log anything in IE8. That's fine, since IE8 has shitty console logging anyway. --- src/core/ReactInstanceHandles.js | 11 +++++++---- src/core/__tests__/ReactInstanceHandles-test.js | 7 +++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/core/ReactInstanceHandles.js b/src/core/ReactInstanceHandles.js index 000856604b..02ea765e0a 100644 --- a/src/core/ReactInstanceHandles.js +++ b/src/core/ReactInstanceHandles.js @@ -296,12 +296,15 @@ var ReactInstanceHandles = { } child = child.nextSibling; } + global.console && console.error && console.error( + 'Error while invoking `findComponentRoot` with the following ' + + 'ancestor node:', + ancestorNode + ); invariant( false, - 'findComponentRoot: Unable to find element by React ID, `%s`. This ' + - 'indicates that someone (or the browser) has mutated the DOM tree in ' + - 'an unexpected way. Try inspecting the child nodes of the element with ' + - 'React ID, `%s`.', + 'findComponentRoot(..., %s): Unable to find element. This probably ' + + 'means the DOM was unexpectedly mutated (e.g. by the browser).', id, ReactID.getID(ancestorNode) ); diff --git a/src/core/__tests__/ReactInstanceHandles-test.js b/src/core/__tests__/ReactInstanceHandles-test.js index 4cde75ab5d..110612c1c8 100644 --- a/src/core/__tests__/ReactInstanceHandles-test.js +++ b/src/core/__tests__/ReactInstanceHandles-test.js @@ -139,10 +139,9 @@ describe('ReactInstanceHandles', function() { ReactID.getID(childNodeB) ); }).toThrow( - 'Invariant Violation: findComponentRoot: Unable to find element by ' + - 'React ID, `.react[0].1:0`. This indicates that someone (or the ' + - 'browser) has mutated the DOM tree in an unexpected way. Try ' + - 'inspecting the child nodes of the element with React ID, `.react[0]`.' + 'Invariant Violation: findComponentRoot(..., .react[0].1:0): Unable ' + + 'to find element. This probably means the DOM was unexpectedly ' + + 'mutated (e.g. by the browser).' ); }); });