69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, '__esModule', {
|
|
value: true
|
|
});
|
|
exports.restoreGlobalErrorHandlers = exports.injectGlobalErrorHandlers = void 0;
|
|
var _state = require('./state');
|
|
/**
|
|
* 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.
|
|
*/
|
|
|
|
const uncaughtExceptionListener = error => {
|
|
(0, _state.dispatchSync)({
|
|
error,
|
|
name: 'error'
|
|
});
|
|
};
|
|
const unhandledRejectionListener = (error, promise) => {
|
|
(0, _state.dispatchSync)({
|
|
error,
|
|
name: 'error',
|
|
promise
|
|
});
|
|
};
|
|
const rejectionHandledListener = promise => {
|
|
(0, _state.dispatchSync)({
|
|
name: 'error_handled',
|
|
promise
|
|
});
|
|
};
|
|
const injectGlobalErrorHandlers = parentProcess => {
|
|
const uncaughtException = process.listeners('uncaughtException').slice();
|
|
const unhandledRejection = process.listeners('unhandledRejection').slice();
|
|
const rejectionHandled = process.listeners('rejectionHandled').slice();
|
|
parentProcess.removeAllListeners('uncaughtException');
|
|
parentProcess.removeAllListeners('unhandledRejection');
|
|
parentProcess.removeAllListeners('rejectionHandled');
|
|
parentProcess.on('uncaughtException', uncaughtExceptionListener);
|
|
parentProcess.on('unhandledRejection', unhandledRejectionListener);
|
|
parentProcess.on('rejectionHandled', rejectionHandledListener);
|
|
return {
|
|
rejectionHandled,
|
|
uncaughtException,
|
|
unhandledRejection
|
|
};
|
|
};
|
|
exports.injectGlobalErrorHandlers = injectGlobalErrorHandlers;
|
|
const restoreGlobalErrorHandlers = (parentProcess, originalErrorHandlers) => {
|
|
parentProcess.removeListener('uncaughtException', uncaughtExceptionListener);
|
|
parentProcess.removeListener(
|
|
'unhandledRejection',
|
|
unhandledRejectionListener
|
|
);
|
|
parentProcess.removeListener('rejectionHandled', rejectionHandledListener);
|
|
for (const listener of originalErrorHandlers.uncaughtException) {
|
|
parentProcess.on('uncaughtException', listener);
|
|
}
|
|
for (const listener of originalErrorHandlers.unhandledRejection) {
|
|
parentProcess.on('unhandledRejection', listener);
|
|
}
|
|
for (const listener of originalErrorHandlers.rejectionHandled) {
|
|
parentProcess.on('rejectionHandled', listener);
|
|
}
|
|
};
|
|
exports.restoreGlobalErrorHandlers = restoreGlobalErrorHandlers;
|