mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
This lets us expose the component stack to the error reporting that happens here as `console.error` patching. Now if you just call `console.error` in the error handlers it'll get the component stack added to the end by React DevTools. However, unfortunately this happens a little too late so the Fiber will be disconnected with its `.return` pointer set to null already. So it'll be too late to extract a parent component stack from but you can at least get the stack from source to error boundary. To work around this I manually add the parent component stack in our default handlers when owner stacks are off. We could potentially fix this but you can also just include it yourself if you're calling `console.error` and it's not a problem for owner stacks. This is not a problem for owner stacks because we'll still have those and so for those just calling `console.error` just works. However, the main feature is that by letting React add them, we can switch to using native error stacks when available.
70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and 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';
|
|
|
|
let suppressWarning = false;
|
|
export function setSuppressWarning(newSuppressWarning) {
|
|
if (__DEV__) {
|
|
suppressWarning = newSuppressWarning;
|
|
}
|
|
}
|
|
|
|
// 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__) {
|
|
if (!suppressWarning) {
|
|
printWarning('warn', format, args);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function error(format, ...args) {
|
|
if (__DEV__) {
|
|
if (!suppressWarning) {
|
|
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 isErrorLogger =
|
|
format === '%s\n\n%s\n' || format === '%o\n\n%s\n\n%s\n';
|
|
|
|
const stack = ReactSharedInternals.getStackAddendum();
|
|
if (stack !== '') {
|
|
format += '%s';
|
|
args = args.concat([stack]);
|
|
}
|
|
|
|
if (isErrorLogger) {
|
|
// Don't prefix our default logging formatting in ReactFiberErrorLoggger.
|
|
// Don't toString the arguments.
|
|
args.unshift(format);
|
|
} else {
|
|
// TODO: Remove this prefix and stop toStringing in the wrapper and
|
|
// instead do it at each callsite as needed.
|
|
// Careful: RN currently depends on this prefix
|
|
// eslint-disable-next-line react-internal/safe-string-coercion
|
|
args = args.map(item => String(item));
|
|
args.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, args);
|
|
}
|
|
}
|