зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1645563 - Introduce devtools helper to swallow rejections under a condition r=nchevobbe
We very often have to introduce a pattern in our async methods in order to "swallow" async errors if a panel was destroyed, if the toolbox was destroyed etc... This is not an issue in a regular usage of the toolbox, but it can lead to many intermittent failures. One solution could be to always wait for all requests to be completed before shutting down tests. Another approach is to have an easy way of swallowing errors when a certain condition is true. The helper added here will run the provided method in a try catch, and will only bubble up errors if the provided check fails. Differential Revision: https://phabricator.services.mozilla.com/D81675
This commit is contained in:
Родитель
3bd96223fe
Коммит
2417b69d3f
|
@ -35,3 +35,33 @@ exports.listenOnce = function listenOnce(element, event, useCapture) {
|
|||
element.addEventListener(event, onEvent, useCapture);
|
||||
});
|
||||
};
|
||||
|
||||
// Return value when `safeAsyncMethod` catches an error.
|
||||
const SWALLOWED_RET = Symbol("swallowed");
|
||||
|
||||
/**
|
||||
* Wraps the provided async method in a try/catch block.
|
||||
* If an error is caught while running the method, check the provided condition
|
||||
* to decide whether the error should bubble or not.
|
||||
*
|
||||
* @param {Function} asyncFn
|
||||
* The async method to wrap.
|
||||
* @param {Function} shouldSwallow
|
||||
* Function that will run when an error is caught. If it returns true,
|
||||
* the error will be swallowed. Otherwise, it will bubble up.
|
||||
* @return {Function} The wrapped method.
|
||||
*/
|
||||
exports.safeAsyncMethod = function(asyncFn, shouldSwallow) {
|
||||
return async function(...args) {
|
||||
try {
|
||||
const ret = await asyncFn(...args);
|
||||
return ret;
|
||||
} catch (e) {
|
||||
if (shouldSwallow()) {
|
||||
console.warn("Async method failed in safeAsyncMethod", e);
|
||||
return SWALLOWED_RET;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
Загрузка…
Ссылка в новой задаче