[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.
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);
});

View File

@@ -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<DiagnosticStepResponse> => {
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}`);

View File

@@ -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<ClientConfig>(
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<Array<{id: string; version: string}>>(
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 = <T>(
url: URL,
callback: (data: T) => void,
url?: URL,
) => {
if (!url) {
return Promise.reject(new Error('Malformed URL'));
}
return new Promise<void>((resolve, reject) => {
getServerAPI<T>(
url,