forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromiseRejectionTrackingOptions.js
54 lines (48 loc) · 1.64 KB
/
promiseRejectionTrackingOptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 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';
type ExtractOptionsType = <P>(((options?: ?P) => void)) => P;
let rejectionTrackingOptions: $Call<ExtractOptionsType, enable> = {
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;