[MM-54584] Fixed an issue where clicking on a link to an unregistered protocol on macOS would cause the app to crash (#2868)

This commit is contained in:
Devin Binnie 2023-10-06 08:29:24 -04:00 committed by GitHub
parent 0a83893a45
commit 013d46da8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 28 deletions

View file

@ -160,6 +160,16 @@ describe('main/allowProtocolDialog', () => {
expect(allowProtocolDialog.allowedProtocols).not.toContain('mattermost:');
expect(fs.writeFile).not.toBeCalled();
});
it('should not throw error when shell.openExternal fails', async () => {
const promise = Promise.resolve({response: 0});
dialog.showMessageBox.mockImplementation(() => promise);
shell.openExternal.mockReturnValue(Promise.reject(new Error('bad protocol')));
allowProtocolDialog.handleDialogEvent('bad-protocol:', 'bad-protocol://community.mattermost.com');
await promise;
expect(shell.openExternal).toBeCalledWith('bad-protocol://community.mattermost.com');
});
});
});
});

View file

@ -44,16 +44,17 @@ export class AllowProtocolDialog {
}
}
handleDialogEvent = (protocol: string, URL: string) => {
handleDialogEvent = async (protocol: string, URL: string) => {
try {
if (this.allowedProtocols.indexOf(protocol) !== -1) {
shell.openExternal(URL);
await shell.openExternal(URL);
return;
}
const mainWindow = MainWindow.get();
if (!mainWindow) {
return;
}
dialog.showMessageBox(mainWindow, {
const {response} = await dialog.showMessageBox(mainWindow, {
title: localizeMessage('main.allowProtocolDialog.title', 'Non http(s) protocol'),
message: localizeMessage('main.allowProtocolDialog.message', '{protocol} link requires an external application.', {protocol}),
detail: localizeMessage('main.allowProtocolDialog.detail', 'The requested link is {URL}. Do you want to continue?', {URL}),
@ -66,7 +67,8 @@ export class AllowProtocolDialog {
],
cancelId: 2,
noLink: true,
}).then(({response}) => {
});
switch (response) {
case 1: {
this.allowedProtocols.push(protocol);
@ -76,16 +78,16 @@ export class AllowProtocolDialog {
}
}
fs.writeFile(allowedProtocolFile, JSON.stringify(this.allowedProtocols), handleError);
shell.openExternal(URL);
await shell.openExternal(URL);
break;
}
case 0:
shell.openExternal(URL);
break;
default:
await shell.openExternal(URL);
break;
}
});
} catch (error) {
log.warn('Could not open external URL', error);
}
}
}