From 4d2a7c69b09141e889f6fe75b1f1484fa0158050 Mon Sep 17 00:00:00 2001 From: Alexander Savelyev <91429106+vordgi@users.noreply.github.com> Date: Tue, 7 May 2024 23:21:52 +0400 Subject: [PATCH] nt-87 improve get-config errors (#88) nt-87 improve get-config errors --- package/src/configuration/getConfig.ts | 47 +++++++++++++------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/package/src/configuration/getConfig.ts b/package/src/configuration/getConfig.ts index a8e85a1..7a8f07e 100644 --- a/package/src/configuration/getConfig.ts +++ b/package/src/configuration/getConfig.ts @@ -8,35 +8,34 @@ const CONFIG_PATH = path.join(process.cwd(), "nimpl-i18n.js"); const dynamicImport = new Function("p", "return import(p)"); const getConfig = async (): Promise => { - try { - if (fs.existsSync(CONFIG_PATH)) { - const config: { default: Config } = await dynamicImport(pathToFileURL(CONFIG_PATH).href); - const { load, getLanguage, languages } = config.default; + if (fs.existsSync(CONFIG_PATH)) { + const config: { default: Config } = await dynamicImport(pathToFileURL(CONFIG_PATH).href); + const { load, getLanguage, languages } = config.default; - if (!load) { - throw new Error( - `Can't find load method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`, - ); - } - - if (!languages) { - throw new Error( - `Can't find languages list in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`, - ); - } + if (!load) { + throw new Error( + `Can't find "load" method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`, + ); + } - if (!getLanguage) { - throw new Error( - `Can't find getLanguage method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`, - ); - } + if (!languages) { + throw new Error( + `Can't find "languages" list in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`, + ); + } - return config.default; + if (!getLanguage) { + throw new Error( + `Can't find "getLanguage" method in configuration file - https://github.com/vordgi/nimpl-i18n#configuration`, + ); } - } catch (err) { - console.error(err); + + return config.default; + } else { + throw new Error( + `Can't load config from "${CONFIG_PATH}". Read more - https://github.com/vordgi/nimpl-i18n#configuration`, + ); } - throw new Error("Can't load config - https://github.com/vordgi/nimpl-i18n#configuration"); }; export default getConfig;