From 6061f9455d0354b7c2cb7583ecb94a361c50630e Mon Sep 17 00:00:00 2001 From: Justin Perez Date: Sun, 10 Apr 2022 06:06:05 -0500 Subject: [PATCH] Typecheck return value of chadrc; propagate errors * Don't blindly assume the only way `require "custom/chadrc.lua" can fail is that the file doesn't exist. * Provide user with a more useful message when return value is wrong --- lua/core/utils.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lua/core/utils.lua b/lua/core/utils.lua index 9c16c53..bfda660 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -141,12 +141,16 @@ end M.load_config = function() local conf = require "core.default_config" - local chadrcExists, change = pcall(require, "custom.chadrc") - - -- if chadrc exists , then merge its table into the default config's - - if chadrcExists then - conf = vim.tbl_deep_extend("force", conf, change) + -- attempt to load and merge a user config + local chadrc_exists = vim.fn.filereadable(vim.fn.stdpath "config" .. "/lua/custom/chadrc.lua") == 1 + if chadrc_exists then + -- merge user config if it exists and is a table; otherwise display an error + local user_config = require "custom.chadrc" + if type(user_config) == 'table' then + conf = vim.tbl_deep_extend("force", conf, user_config) + else + error("User config (chadrc.lua) *must* return a table!") + end end return conf