Skip to content
10 changes: 7 additions & 3 deletions packages/react-native/Libraries/LogBox/Data/LogBoxData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
*/

('use strict');

import type {CodeFrame} from '../../Core/Devtools/symbolicateStackTrace';
import type {ExtendedError} from '../../Core/ExtendedError';
import type {LogLevel} from './LogBoxLog';
import type {Stack} from './LogBoxSymbolication';
import type {
Category,
ComponentStack,
Expand All @@ -30,6 +31,8 @@ export type LogData = $ReadOnly<{|
message: Message,
category: Category,
componentStack: ComponentStack,
codeFrame?: ?CodeFrame,
stack?: Stack,
|}>;

export type Observer = (
Expand Down Expand Up @@ -198,16 +201,17 @@ export function addLog(log: LogData): void {
// otherwise spammy logs would pause rendering.
setImmediate(() => {
try {
const stack = parseErrorStack(errorForStackTrace?.stack);
const defaultStack = parseErrorStack(errorForStackTrace?.stack);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this to reuse the same parsing logic for both

Suggested change
const defaultStack = parseErrorStack(errorForStackTrace?.stack);
const stack = parseErrorStack(log.stack ?? errorForStackTrace.stack);


appendNewLog(
new LogBoxLog({
level: log.level,
message: log.message,
isComponentError: false,
stack,
stack: log.stack ?? defaultStack,
category: log.category,
componentStack: log.componentStack,
codeFrame: log.codeFrame,
}),
);
} catch (error) {
Expand Down
49 changes: 47 additions & 2 deletions packages/react-native/Libraries/promiseRejectionTrackingOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

import typeof {enable} from 'promise/setimmediate/rejection-tracking';

let rejectionTrackingOptions: $NonMaybeType<Parameters<enable>[0]> = {
type ExtractOptionsType = <P>(((options?: ?P) => void)) => P;

let rejectionTrackingOptions: $Call<ExtractOptionsType, enable> = {
allRejections: true,
onUnhandled: (id, rejection = {}) => {
let message: string;
Expand All @@ -22,7 +24,50 @@ let rejectionTrackingOptions: $NonMaybeType<Parameters<enable>[0]> = {
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
message = Error.prototype.toString.call(rejection);
const error: Error = (rejection: $FlowFixMe);
stack = error.stack;

// Print pretty unhandled rejections while on DEV
if (__DEV__) {
const parseErrorStack = require('./Core/Devtools/parseErrorStack');
const symbolicateStackTrace = require('./Core/Devtools/symbolicateStackTrace');
const LogBox = require('./LogBox/LogBox').default;

const parsedStack = parseErrorStack(error.stack);

symbolicateStackTrace(parsedStack)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't live here. Logbox can handle this better by first showing the unsymbolicated stacktrace and then replacing the relevant log when the symbolication is ready.

.then(prettyStack => {
let warning =
`Possible Unhandled Promise Rejection (id: ${id}):\n` +
`${message ?? ''}\n`;

if (prettyStack.codeFrame != null) {
warning += `at File: ${prettyStack.codeFrame.fileName}, row: ${
prettyStack.codeFrame.location?.row ?? 'Unknown'
}, column: ${
prettyStack.codeFrame.location?.column ?? 'Unknown'
}`;

LogBox.addLog({
level: 'warn',
message: {content: warning, substitutions: []},
componentStack: [],
codeFrame: prettyStack.codeFrame,
category: `${prettyStack.codeFrame.fileName}-${
prettyStack.codeFrame.location?.row ?? 'unknown'
}-${prettyStack.codeFrame.location?.column ?? 'unknown'}`,
});
} else {
console.warn(warning);
}
})
.catch(() => {
const warning =
`Possible Unhandled Promise Rejection (id: ${id}):\n` +
`${message ?? ''}\n` +
(stack == null ? '' : stack);
console.warn(warning);
});
return;
}
} else {
try {
message = require('pretty-format')(rejection);
Expand Down