[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
This commit is contained in:
Devin Binnie
2023-06-26 08:48:42 -04:00
committed by GitHub
parent f51a7d31db
commit 850573326c
3 changed files with 16 additions and 9 deletions

View File

@@ -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. // The whole response has been received. Print out the result.
resp.on('end', () => { resp.on('end', () => {
logger.debug('resp.on.end', {data}); logger.debug('resp.on.end', {data, url});
const respBody = JSON.parse(data); if (data.length) {
if (respBody.status === 'OK') { const respBody = JSON.parse(data);
resolve(true); if (respBody.status === 'OK') {
return; resolve(true);
return;
}
} }
resolve(false); resolve(false);
}); });

View File

@@ -5,6 +5,7 @@ import {ElectronLog} from 'electron-log';
import {DiagnosticStepResponse} from 'types/diagnostics'; import {DiagnosticStepResponse} from 'types/diagnostics';
import ServerManager from 'common/servers/serverManager'; import ServerManager from 'common/servers/serverManager';
import {parseURL} from 'common/utils/url';
import DiagnosticsStep from '../DiagnosticStep'; import DiagnosticsStep from '../DiagnosticStep';
@@ -24,7 +25,7 @@ const run = async (logger: ElectronLog): Promise<DiagnosticStepResponse> => {
throw new Error(`Invalid server configuration. Server Url: ${server.url}, server name: ${server.name}`); 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) { if (!serverOnline) {
throw new Error(`Server appears to be offline. Server url: ${server.url}`); throw new Error(`Server appears to be offline. Server url: ${server.url}`);

View File

@@ -4,6 +4,7 @@
import {ClientConfig, RemoteInfo} from 'types/server'; import {ClientConfig, RemoteInfo} from 'types/server';
import {MattermostServer} from 'common/servers/MattermostServer'; import {MattermostServer} from 'common/servers/MattermostServer';
import {parseURL} from 'common/utils/url';
import {getServerAPI} from './serverAPI'; import {getServerAPI} from './serverAPI';
@@ -18,8 +19,8 @@ export class ServerInfo {
fetchConfigData = async () => { fetchConfigData = async () => {
await this.getRemoteInfo<ClientConfig>( await this.getRemoteInfo<ClientConfig>(
new URL(`${this.server.url.toString()}/api/v4/config/client?format=old`),
this.onGetConfig, this.onGetConfig,
parseURL(`${this.server.url}/api/v4/config/client?format=old`),
); );
return this.remoteInfo; return this.remoteInfo;
@@ -28,17 +29,20 @@ export class ServerInfo {
fetchRemoteInfo = async () => { fetchRemoteInfo = async () => {
await this.fetchConfigData(); await this.fetchConfigData();
await this.getRemoteInfo<Array<{id: string; version: string}>>( await this.getRemoteInfo<Array<{id: string; version: string}>>(
new URL(`${this.server.url.toString()}/api/v4/plugins/webapp`),
this.onGetPlugins, this.onGetPlugins,
parseURL(`${this.server.url}/api/v4/plugins/webapp`),
); );
return this.remoteInfo; return this.remoteInfo;
} }
private getRemoteInfo = <T>( private getRemoteInfo = <T>(
url: URL,
callback: (data: T) => void, callback: (data: T) => void,
url?: URL,
) => { ) => {
if (!url) {
return Promise.reject(new Error('Malformed URL'));
}
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
getServerAPI<T>( getServerAPI<T>(
url, url,