MCPcopy Index your code
hub / github.com/react/react / patchConsoleForErrorsAndWarnings

Function patchConsoleForErrorsAndWarnings

packages/react-devtools-shared/src/hook.js:432–614  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

430
431 // For Errors and Warnings we only patch console once
432 function patchConsoleForErrorsAndWarnings() {
433 // Don't patch console in case settings were not injected
434 if (!hook.settings) {
435 return;
436 }
437
438 const consoleMethodsToOverrideForErrorsAndWarnings = [
439 'error',
440 'trace',
441 'warn',
442 ];
443
444 // eslint-disable-next-line no-for-of-loops/no-for-of-loops
445 for (const method of consoleMethodsToOverrideForErrorsAndWarnings) {
446 const originalMethod = targetConsole[method];
447 const overrideMethod: (...args: Array<any>) => void = (...args) => {
448 const settings = hook.settings;
449 // Something unexpected happened, fallback to just printing the console message.
450 if (settings == null) {
451 originalMethod(...args);
452 return;
453 }
454
455 if (
456 isRunningDuringStrictModeInvocation &&
457 settings.hideConsoleLogsInStrictMode
458 ) {
459 return;
460 }
461
462 let injectedComponentStackAsFakeError = false;
463 let alreadyHasComponentStack = false;
464 if (settings.appendComponentStack) {
465 const lastArg = args.length > 0 ? args[args.length - 1] : null;
466 alreadyHasComponentStack =
467 typeof lastArg === 'string' && isStringComponentStack(lastArg); // The last argument should be a component stack.
468 }
469
470 const shouldShowInlineWarningsAndErrors =
471 settings.showInlineWarningsAndErrors &&
472 (method === 'error' || method === 'warn');
473
474 // Search for the first renderer that has a current Fiber.
475 // We don't handle the edge case of stacks for more than one (e.g. interleaved renderers?)
476 // eslint-disable-next-line no-for-of-loops/no-for-of-loops
477 for (const rendererInterface of hook.rendererInterfaces.values()) {
478 const {onErrorOrWarning, getComponentStack} = rendererInterface;
479 try {
480 if (shouldShowInlineWarningsAndErrors) {
481 // patch() is called by two places: (1) the hook and (2) the renderer backend.
482 // The backend is what implements a message queue, so it's the only one that injects onErrorOrWarning.
483 if (onErrorOrWarning != null) {
484 onErrorOrWarning(
485 ((method: any): 'error' | 'warn'),
486 args.slice(),
487 );
488 }
489 }

Callers 1

installHookFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected