Do not reject error in mozAddonManager.install() (#8667)

This commit is contained in:
William Durand 2019-09-25 22:33:42 +02:00 коммит произвёл GitHub
Родитель cfa3978237
Коммит 86155f3794
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 35 добавлений и 20 удалений

Просмотреть файл

@ -102,17 +102,21 @@ export function getAddon(
type OptionalInstallParams = {|
...OptionalParams,
src: string,
_log?: typeof log,
hash?: string | null,
onIgnoredRejection?: () => void,
src: string,
|};
export function install(
_url: string | void,
eventCallback: Function,
{
_log = log,
_mozAddonManager = window.navigator.mozAddonManager,
src,
hash,
onIgnoredRejection = () => {},
src,
}: OptionalInstallParams = {},
) {
if (src === undefined) {
@ -123,15 +127,22 @@ export function install(
return _mozAddonManager.createInstall({ url, hash }).then((installObj) => {
const callback = (e) => eventCallback(installObj, e);
for (const event of INSTALL_EVENT_LIST) {
log.info(`[install] Adding listener for ${event}`);
_log.info(`[install] Adding listener for ${event}`);
installObj.addEventListener(event, callback);
}
return new Promise((resolve, reject) => {
installObj.addEventListener('onInstallEnded', () => resolve());
installObj.addEventListener('onInstallFailed', () => reject());
log.info('Events to handle the installation initialized.');
// See: https://github.com/mozilla/addons-frontend/issues/8633
installObj.install().catch(reject);
_log.info('Events to handle the installation initialized.');
installObj.install().catch((error) => {
// The `mozAddonManager` has events we can listen to, this error occurs
// when a user cancels the installation but we are already notified via
// `onInstallCancelled`.
// See: https://github.com/mozilla/addons-frontend/issues/8668
_log.warn(`Ignoring promise rejection during installation: ${error}`);
onIgnoredRejection();
});
});
});
}

Просмотреть файл

@ -197,23 +197,27 @@ describe(__filename, () => {
);
});
it('rejects if the install returns an error', () => {
const error = new Error('oops');
// See: https://github.com/mozilla/addons-frontend/issues/8633
it('logs and ignores rejected install errors', async () => {
let finishInstall;
const installToFinish = new Promise((resolve) => {
finishInstall = resolve;
});
const _log = getFakeLogger();
fakeInstallObj.install = sinon.spy(() => {
return Promise.reject(error);
return Promise.reject(new Error('oops'));
});
return (
addonManager
.install(fakeInstallUrl, fakeCallback, {
_mozAddonManager: fakeMozAddonManager,
src: 'home',
})
// The second argument is the reject function.
.then(unexpectedSuccess, () => {
sinon.assert.calledOnce(fakeInstallObj.install);
})
);
addonManager.install(fakeInstallUrl, fakeCallback, {
_log,
_mozAddonManager: fakeMozAddonManager,
onIgnoredRejection: () => finishInstall(),
src: 'home',
});
await installToFinish;
sinon.assert.calledOnce(_log.warn);
});
it('passes the installObj, the event and the id to the callback', async () => {