From bf5618248e9f182116cf6dee561482fd7579b363 Mon Sep 17 00:00:00 2001 From: Jakob Niggel Date: Fri, 13 Sep 2024 11:09:24 +0200 Subject: [PATCH] feat: add better error handling --- lib/config.mjs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/config.mjs b/lib/config.mjs index e18fdd9..2170557 100644 --- a/lib/config.mjs +++ b/lib/config.mjs @@ -86,10 +86,16 @@ export async function writeProfile(profileName, profileConfig) { await writeFile(configFile, ini.stringify(config)); } -export async function getSession(sessioName) { +export async function getSession(sessionName) { const config = ini.parse(await readFile(configFile, "utf-8")); + const key = ("sso-session " + sessionName).trim(); - return config["sso-session " + sessioName]; + if (!config[key]) { + console.log(`Key "${key}" not found in ${configFile}`); + process.exit(1); + } + + return config[key]; } export async function getCurrentSession() { @@ -137,7 +143,12 @@ export async function readAccountCache(sessionName) { } catch (_) { return []; } - return JSON.parse(await readFile(cacheFile, "utf8")); + + try { + return JSON.parse(await readFile(cacheFile, "utf8")); + } catch (_) { + return []; + } } export async function writeAccountCache(sessionName, accounts) {