Cannot implement error handling on `require` properly

I created a helper function called safe_require(), it’s very simple, basically what is does is make a protected call to the desired module, and if anything goes wrong, an error is thrown with vim.notify() and further execution is gracefully aborted, code below:

safe_require.lua:

local function safe_require(module_name)
    local status_ok, module = pcall(require, module_name)
    if not status_ok then
        vim.notify("Couldn't load module '" .. module_name .. "'")
        do return end
    end
    return module
end

But, whenever i try to use it on the config field of packer plugins, it fails with the following message:

packer.nvim: Error running config for <plugin_name>: [string "..."]:0: attempt to call upvalue '' (a nil value)

Example of how i’m doing it below:

local safe_require = require "config.helpers.safe_require"

local packer = safe_require "packer"

packer.startup(function(use)
    use {"windwp/nvim-autopairs", config = function()
        -- this file is responsible for bootstrapping `nvim-autopairs`
        safe_require "config.plugins.nvim-autopairs"
    end}
end)

Any clue of what i might be missing?

Solved it by encapsulating the safe_require helper on a global namespace, this also has the benefit of removing the need of importing this helper every time it’s needed, example below:

Config = {}
Config.safe_require = function(...)
end

Config.safe_require(...)

Credits to PaperCupsAhoy on Reddit