From 2f8c4a9ab3c3f53e2065e54ad3fb4e19291f367e Mon Sep 17 00:00:00 2001 From: James Lyne Date: Tue, 18 May 2021 18:27:11 +0100 Subject: [PATCH] Make configuration validation sync --- src/api.ts | 57 +++++++++++++++++--------------- src/errors/ConfigurationError.ts | 22 ++++++++++++ src/main.ts | 8 +++-- 3 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 src/errors/ConfigurationError.ts diff --git a/src/api.ts b/src/api.ts index eff586c..643d3bb 100644 --- a/src/api.ts +++ b/src/api.ts @@ -37,6 +37,7 @@ import { import {useStore} from "@/store"; import ChatError from "@/errors/ChatError"; import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index"; +import ConfigurationError from "@/errors/ConfigurationError"; const titleColours = /ยง[0-9a-f]/ig; @@ -555,12 +556,12 @@ function buildUpdates(data: Array): DynmapUpdates { return updates; } -const validateLiveAtlasConfiguration = (config: any): Promise> => { +const validateLiveAtlasConfiguration = (config: any): Map => { const check = '\nCheck your server configuration in index.html is correct.', result = new Map(); if (!Object.keys(config).length) { - return Promise.reject(`No servers defined in LiveAtlas configuration.`); + throw new ConfigurationError(`No servers defined. ${check}`); } for (const server in config) { @@ -570,35 +571,35 @@ const validateLiveAtlasConfiguration = (config: any): Promise> => { +const validateDynmapConfiguration = (config: DynmapUrlConfig): Map => { const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; if (!config) { - return Promise.reject(`Dynmap configuration is missing. ${check}`); + throw new ConfigurationError(`Dynmap configuration is missing. ${check}`); } if (!config.configuration) { - return Promise.reject(`Dynmap configuration URL is missing. ${check}`); + throw new ConfigurationError(`Dynmap configuration URL is missing. ${check}`); } if (!config.update) { - return Promise.reject(`Dynmap update URL is missing. ${check}`); + throw new ConfigurationError(`Dynmap update URL is missing. ${check}`); } if (!config.markers) { - return Promise.reject(`Dynmap markers URL is missing. ${check}`); + throw new ConfigurationError(`Dynmap markers URL is missing. ${check}`); } if (!config.tiles) { - return Promise.reject(`Dynmap tiles URL is missing. ${check}`); + throw new ConfigurationError(`Dynmap tiles URL is missing. ${check}`); } if (!config.sendmessage) { - return Promise.reject(`Dynmap sendmessage URL is missing. ${check}`); + throw new ConfigurationError(`Dynmap sendmessage URL is missing. ${check}`); } const result = new Map(); @@ -655,7 +656,7 @@ const validateDynmapConfiguration = (config: DynmapUrlConfig): Promise> { - if (typeof window.liveAtlasConfig.servers !== 'undefined') { - return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers); + validateConfiguration(): Map { + if (!window.liveAtlasConfig) { + throw new ConfigurationError(`Configuration object is missing`); } - return validateDynmapConfiguration(window.config.url ?? null); + if (typeof window.liveAtlasConfig.servers !== 'undefined') { + return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers || {}); + } + + return validateDynmapConfiguration(window.config?.url || null); }, async getConfiguration(): Promise { diff --git a/src/errors/ConfigurationError.ts b/src/errors/ConfigurationError.ts new file mode 100644 index 0000000..a218015 --- /dev/null +++ b/src/errors/ConfigurationError.ts @@ -0,0 +1,22 @@ +/* + * Copyright 2021 James Lyne + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default class ConfigurationError extends Error { + constructor(message: string) { + super(message); + this.name = "ConfigurationError"; + } +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 9926929..7ec999e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -85,7 +85,9 @@ window.showSplashError = function(message: string, fatal: boolean, attempts: num console.info(`LiveAtlas version ${store.state.version} - https://github.com/JLyne/LiveAtlas`); -API.validateConfiguration().then((config) => { +try { + const config = API.validateConfiguration(); + store.commit(MutationTypes.SET_SERVERS, config); if(config.size > 1) { @@ -106,7 +108,7 @@ API.validateConfiguration().then((config) => { // app.config.performance = true; app.mount('#app'); -}).catch(e => { +} catch(e) { console.error('LiveAtlas configuration is invalid: ', e); window.showSplashError('LiveAtlas configuration is invalid\n' + e, true); -}); +}