From 60e29c0fb4c7bd104ea7ae2c7bbe2912beaf833a Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Mon, 4 Mar 2024 21:37:31 +0100 Subject: [PATCH] fix(utils): update try_require signature --- lua/utils.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lua/utils.lua b/lua/utils.lua index 9681e2d..6c466a4 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -83,15 +83,20 @@ function M.err(msg, title) M.notify(msg, title, vim.log.levels.ERROR) end +--- Attempts to load a module and logs errors on failure. +---@param module string The module to attempt to load. +---@param err_title string The error message title. +---@param on_success fun(module: unknown)? Will be called if module was loaded. +---@return unknown? module The loaded module if successful, otherwise nil. function M.try_require(module, err_title, on_success) local has_module, resp = pcall(require, module) if has_module then - if not on_success then - return + if on_success then + on_success(resp) end - return on_success(resp) + return resp end M.err(("Failed to load module %s"):format(module), err_title)