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
3 changes: 2 additions & 1 deletion packages/react-native/Libraries/LogBox/Data/LogBoxData.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type LogData = $ReadOnly<{|
message: Message,
category: Category,
componentStack: ComponentStack,
stack?: string,
|}>;

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 = parseErrorStack(log.stack ?? errorForStackTrace?.stack);

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

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

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 +24,26 @@ 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__) {
LogBox.addLog({
level: 'warn',
message: {content: warning, substitutions: []},
componentStack: [],
stack: error.stack,
category: 'possible_unhandled_promise_rejection',
});

return;
} else {
console.warn(warning);
}
Copy link
Member

Choose a reason for hiding this comment

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

We never write to stack anymore, can you cleanup warning?

And I think this will log twice in !__DEV__ (although this module is only ever used if (__DEV__), so we could remove this check altogether really.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, reverted the double warning and just handled the special case while on dev

} else {
try {
message = require('pretty-format')(rejection);
Expand Down