From 850573326c08549f383d0a10574912558b1553d8 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Mon, 26 Jun 2023 08:48:42 -0400 Subject: [PATCH] [MM-53295] Fix crash in diagnostics when server is unreachable (#2771) * [MM-53295] Fix crash in diagnostics when server is unreachable * Fix URL parsing, lint fixes --- src/main/diagnostics/steps/internal/utils.ts | 12 +++++++----- .../diagnostics/steps/step3.serverConnectivity.ts | 3 ++- src/main/server/serverInfo.ts | 10 +++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/diagnostics/steps/internal/utils.ts b/src/main/diagnostics/steps/internal/utils.ts index 8b52b378..ad2f8584 100644 --- a/src/main/diagnostics/steps/internal/utils.ts +++ b/src/main/diagnostics/steps/internal/utils.ts @@ -70,11 +70,13 @@ export async function isOnline(logger: ElectronLog = log, url = IS_ONLINE_ENDPOI // The whole response has been received. Print out the result. resp.on('end', () => { - logger.debug('resp.on.end', {data}); - const respBody = JSON.parse(data); - if (respBody.status === 'OK') { - resolve(true); - return; + logger.debug('resp.on.end', {data, url}); + if (data.length) { + const respBody = JSON.parse(data); + if (respBody.status === 'OK') { + resolve(true); + return; + } } resolve(false); }); diff --git a/src/main/diagnostics/steps/step3.serverConnectivity.ts b/src/main/diagnostics/steps/step3.serverConnectivity.ts index 1b6e8209..3021e47b 100644 --- a/src/main/diagnostics/steps/step3.serverConnectivity.ts +++ b/src/main/diagnostics/steps/step3.serverConnectivity.ts @@ -5,6 +5,7 @@ import {ElectronLog} from 'electron-log'; import {DiagnosticStepResponse} from 'types/diagnostics'; import ServerManager from 'common/servers/serverManager'; +import {parseURL} from 'common/utils/url'; import DiagnosticsStep from '../DiagnosticStep'; @@ -24,7 +25,7 @@ const run = async (logger: ElectronLog): Promise => { throw new Error(`Invalid server configuration. Server Url: ${server.url}, server name: ${server.name}`); } - const serverOnline = await isOnline(logger, `${server.url}/api/v4/system/ping`); + const serverOnline = await isOnline(logger, parseURL(`${server.url}/api/v4/system/ping`)?.toString()); if (!serverOnline) { throw new Error(`Server appears to be offline. Server url: ${server.url}`); diff --git a/src/main/server/serverInfo.ts b/src/main/server/serverInfo.ts index f3a5ad00..7dbff9ef 100644 --- a/src/main/server/serverInfo.ts +++ b/src/main/server/serverInfo.ts @@ -4,6 +4,7 @@ import {ClientConfig, RemoteInfo} from 'types/server'; import {MattermostServer} from 'common/servers/MattermostServer'; +import {parseURL} from 'common/utils/url'; import {getServerAPI} from './serverAPI'; @@ -18,8 +19,8 @@ export class ServerInfo { fetchConfigData = async () => { await this.getRemoteInfo( - new URL(`${this.server.url.toString()}/api/v4/config/client?format=old`), this.onGetConfig, + parseURL(`${this.server.url}/api/v4/config/client?format=old`), ); return this.remoteInfo; @@ -28,17 +29,20 @@ export class ServerInfo { fetchRemoteInfo = async () => { await this.fetchConfigData(); await this.getRemoteInfo>( - new URL(`${this.server.url.toString()}/api/v4/plugins/webapp`), this.onGetPlugins, + parseURL(`${this.server.url}/api/v4/plugins/webapp`), ); return this.remoteInfo; } private getRemoteInfo = ( - url: URL, callback: (data: T) => void, + url?: URL, ) => { + if (!url) { + return Promise.reject(new Error('Malformed URL')); + } return new Promise((resolve, reject) => { getServerAPI( url,