Files
react-native/packages/react-native/Libraries/promiseRejectionTrackingOptions.js
T
Sam Zhou 605db4427c Replace $Call in react-native (#38104)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/38104

With the introduction of [new utility types](https://flow.org/en/docs/types/utilities/#toc-return-type) in Flow, these $Call are no longer needed

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D47078502

fbshipit-source-id: 7fc5f6aa08052b8de8e3f0a45ce2d7a24cb4e1b5
2023-06-28 07:18:53 -07:00

53 lines
1.6 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.
*
* @format
* @flow strict
*/
import typeof {enable} from 'promise/setimmediate/rejection-tracking';
let rejectionTrackingOptions: $NonMaybeType<Parameters<enable>[0]> = {
allRejections: true,
onUnhandled: (id, rejection = {}) => {
let message: string;
let stack: ?string;
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
const stringValue = Object.prototype.toString.call(rejection);
if (stringValue === '[object Error]') {
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
message = Error.prototype.toString.call(rejection);
const error: Error = (rejection: $FlowFixMe);
stack = error.stack;
} else {
try {
message = require('pretty-format')(rejection);
} catch {
message =
typeof rejection === 'string'
? rejection
: JSON.stringify((rejection: $FlowFixMe));
}
}
const warning =
`Possible Unhandled Promise Rejection (id: ${id}):\n` +
`${message ?? ''}\n` +
(stack == null ? '' : stack);
console.warn(warning);
},
onHandled: id => {
const warning =
`Promise Rejection Handled (id: ${id})\n` +
'This means you can ignore any previous messages of the form ' +
`"Possible Unhandled Promise Rejection (id: ${id}):"`;
console.warn(warning);
},
};
export default rejectionTrackingOptions;