summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-11-29 20:55:10 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-11-29 20:55:10 +1100
commitf202ffec313a56e307faf758697c4d66a8c55093 (patch)
tree581d9cf50eb0c2fb46c885dcffcf0f9ba83fe955 /src
parentfucking postgres (diff)
downloadserver-f202ffec313a56e307faf758697c4d66a8c55093.tar.xz
Proper error messages for invalid config
Diffstat (limited to '')
-rw-r--r--src/util/util/Config.ts39
1 files changed, 35 insertions, 4 deletions
diff --git a/src/util/util/Config.ts b/src/util/util/Config.ts
index caf4a1e1..2624a60a 100644
--- a/src/util/util/Config.ts
+++ b/src/util/util/Config.ts
@@ -16,11 +16,11 @@
 	along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
 
-import { ConfigEntity } from "../entities/Config";
-import fs from "fs/promises";
 import { existsSync } from "fs";
-import { ConfigValue } from "../config";
+import fs from "fs/promises";
 import { OrmUtils } from "..";
+import { ConfigValue } from "../config";
+import { ConfigEntity } from "../entities/Config";
 
 // TODO: yaml instead of json
 const overridePath = process.env.CONFIG_PATH ?? "";
@@ -36,7 +36,7 @@ export const Config = {
 		if (config) return config;
 		console.log("[Config] Loading configuration...");
 		if (!process.env.CONFIG_PATH) {
-			pairs = await ConfigEntity.find();
+			pairs = await validateConfig();
 			config = pairsToConfig(pairs);
 		} else {
 			console.log(`[Config] Using CONFIG_PATH rather than database`);
@@ -129,3 +129,34 @@ function pairsToConfig(pairs: ConfigEntity[]) {
 
 	return value as ConfigValue;
 }
+
+const validateConfig = async () => {
+	let hasErrored = false;
+	const config = await ConfigEntity.find({ select: { key: true } });
+
+	for (const row in config) {
+		try {
+			const found = await ConfigEntity.findOne({
+				where: { key: config[row].key },
+			});
+			if (!found) continue;
+			config[row] = found;
+		} catch (e) {
+			console.error(
+				`Config key '${config[row].key}' has invalid JSON value : ${
+					(e as Error)?.message
+				}`,
+			);
+			hasErrored = true;
+		}
+	}
+
+	if (hasErrored) {
+		console.error(
+			"Your config has invalid values. Fix them first https://docs.spacebar.chat/setup/server/configuration",
+		);
+		process.exit(1);
+	}
+
+	return config;
+};