mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
This was originally added so you could use "break on caught exceptions" but that feature is pretty useless these days since it's used for feature detection and Suspense. The better pattern is to use the stack trace, jump to source and set a break point here. Since DevTools injects its own console.error, we could inject a "debugger" statement in there. Conditionally. E.g. React DevTools could have a flag to toggle "break on warnings".
48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import ReactSharedInternals from 'shared/ReactSharedInternals';
|
|
|
|
// In DEV, calls to console.warn and console.error get replaced
|
|
// by calls to these methods by a Babel plugin.
|
|
//
|
|
// In PROD (or in packages without access to React internals),
|
|
// they are left as they are instead.
|
|
|
|
export function warn(format, ...args) {
|
|
if (__DEV__) {
|
|
printWarning('warn', format, args);
|
|
}
|
|
}
|
|
|
|
export function error(format, ...args) {
|
|
if (__DEV__) {
|
|
printWarning('error', format, args);
|
|
}
|
|
}
|
|
|
|
function printWarning(level, format, args) {
|
|
// When changing this logic, you might want to also
|
|
// update consoleWithStackDev.www.js as well.
|
|
if (__DEV__) {
|
|
const ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
|
|
const stack = ReactDebugCurrentFrame.getStackAddendum();
|
|
if (stack !== '') {
|
|
format += '%s';
|
|
args = args.concat([stack]);
|
|
}
|
|
|
|
const argsWithFormat = args.map(item => '' + item);
|
|
// Careful: RN currently depends on this prefix
|
|
argsWithFormat.unshift('Warning: ' + format);
|
|
// We intentionally don't use spread (or .apply) directly because it
|
|
// breaks IE9: https://github.com/facebook/react/issues/13610
|
|
// eslint-disable-next-line react-internal/no-production-logging
|
|
Function.prototype.apply.call(console[level], console, argsWithFormat);
|
|
}
|
|
}
|