Skip to content

Commit

Permalink
Improve low priority warning (facebook#9754)
Browse files Browse the repository at this point in the history
* Add back caught error and other checks to 'lowPriorityWarning'

**what is the change?:**
This change makes 'lowPriorityWarning' an exact copy of 'warning.js' from
https://github.com/facebook/fbjs/blob/e66ba20ad5be433eb54423f2b097d829324d9de6/packages/fbjs/src/__forks__/warning.js
where before we had skipped some checks from that module.

- Adds an error which we catch, in order to let people find the error and resulting stack trace when using devtools with 'pause on caught errors' checked.
- Adds check that 'format' argument is passed

**why make this change?:**
- To maintain a closer fork to 'warning.js'
- To allow easier debugging using 'pause on caught errors'
- To validate inputs to 'lowPriorityWarning'

**test plan:**
`yarn test`
  • Loading branch information
flarnie committed Jun 7, 2017
1 parent 80e87c4 commit 3911544
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/shared/utils/lowPriorityWarning.js
Expand Up @@ -19,7 +19,6 @@
* and do nothing when 'console' is not supported.
* This really simplifies the code.
* ---
*
* Similar to invariant but only logs a warning if the condition is not met.
* This can be used to log issues in development environments in critical
* paths. Removing the logging code for production environments will keep the
Expand All @@ -29,12 +28,30 @@
var lowPriorityWarning = function() {};

if (__DEV__) {
lowPriorityWarning = function(condition, format, ...args) {
const printWarning = function(format, ...args) {
var argIndex = 0;
var message = 'Warning: ' + format.replace(/%s/g, () => args[argIndex++]);
if (!condition && typeof console !== 'undefined') {
if (typeof console !== 'undefined') {
console.warn(message);
}
try {
// --- Welcome to debugging React ---
// This error was thrown as a convenience so that you can use this stack
// to find the callsite that caused this warning to fire.
throw new Error(message);
} catch (x) {}
};

lowPriorityWarning = function(condition, format, ...args) {
if (format === undefined) {
throw new Error(
'`warning(condition, format, ...args)` requires a warning ' +
'message argument',
);
}
if (!condition) {
printWarning(format, ...args);
}
};
}

Expand Down

0 comments on commit 3911544

Please sign in to comment.