Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbolicate unhandled promise rejections #40914

Closed
wants to merge 12 commits into from
5 changes: 3 additions & 2 deletions packages/react-native/Libraries/LogBox/Data/LogBoxData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/

('use strict');

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

export type Observer = (
Expand Down Expand Up @@ -198,7 +199,7 @@ export function addLog(log: LogData): void {
// otherwise spammy logs would pause rendering.
setImmediate(() => {
try {
const stack = parseErrorStack(errorForStackTrace?.stack);
const stack = log.stack ?? parseErrorStack(errorForStackTrace?.stack);

appendNewLog(
new LogBoxLog({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

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

const parseErrorStack = require('./Core/Devtools/parseErrorStack');
const LogBox = require('./LogBox/LogBox').default;
Copy link
Member

Choose a reason for hiding this comment

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

Use import instead of require here


let rejectionTrackingOptions: $NonMaybeType<Parameters<enable>[0]> = {
allRejections: true,
onUnhandled: (id, rejection = {}) => {
Expand All @@ -22,7 +25,28 @@ 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;

const warning =
`Possible Unhandled Promise Rejection (id: ${id}):\n` +
`${message ?? ''}\n` +
(stack == null ? '' : stack);

// Print pretty unhandled rejections while on DEV
if (__DEV__) {
const parsedStack = parseErrorStack(error.stack);

LogBox.addLog({
level: 'warn',
message: {content: warning, substitutions: []},
componentStack: [],
stack: parsedStack,
category: 'possible_unhandled_promise_rejection',
});
Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid parsing the error stack here, and leave that op to LogBox?

Consider whether addException may be better here, since it already supports passing in a stack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just tried but addException tackes an Array<StackFrame> on the stack property not a string. Tried modifying that but getting another error:

Oscar Franco Screen 000116

So it's a chicken and egg problem trying to use that. I can modify addLog to check for a string stack and parsing it there though, would that be a good compromise?


return;
} else {
console.warn(warning);
}
} else {
try {
message = require('pretty-format')(rejection);
Expand Down