44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
var boundRejectionHandler = null;
|
|
|
|
function rejectionHandler(callback, e) {
|
|
if (e == null || e.reason == null) {
|
|
return callback(new Error('Unknown'));
|
|
}
|
|
var reason = e.reason;
|
|
|
|
if (reason instanceof Error) {
|
|
return callback(reason);
|
|
}
|
|
// A non-error was rejected, we don't have a trace :(
|
|
// Look in your browser's devtools for more information
|
|
return callback(new Error(reason));
|
|
}
|
|
|
|
function registerUnhandledRejection(target, callback) {
|
|
if (boundRejectionHandler !== null) {
|
|
return;
|
|
}
|
|
boundRejectionHandler = rejectionHandler.bind(undefined, callback);
|
|
// $FlowFixMe
|
|
target.addEventListener('unhandledrejection', boundRejectionHandler);
|
|
}
|
|
|
|
function unregisterUnhandledRejection(target) {
|
|
if (boundRejectionHandler === null) {
|
|
return;
|
|
}
|
|
// $FlowFixMe
|
|
target.removeEventListener('unhandledrejection', boundRejectionHandler);
|
|
boundRejectionHandler = null;
|
|
}
|
|
|
|
export { registerUnhandledRejection as register, unregisterUnhandledRejection as unregister }; |