'How can function arguments, be put in some sort of protected environment, without pcall nesting?

I've made an example code here, to explain my question better.

local t = {}

function printTest(...)
    print(...)
end

The issue I want to solve is to catch an error, when calling this function printTest() with any error causing arguments, as example: printTest(t.example.example). But without having to nest printTest() itself inside a pcall().

What I was mainly trying to do with this code, is that I wanted to make some sort of new console output, that will log things, whenever printTest() gets called, without touching the persisting console environment.

However the issue were that errors would still end up to the main console.

So I tried, doing this because I wanted to see if I could somehow have a pcall_function

otherTest = function(...)
    local status, response = pcall(test, ...)

    if (status == false) then
        print(response)
    end
end

However, calling otherTest.(t.example.example) gives the error

attempt to index a nil value (field 'example')

just like how printTest(t.example.example) would.

 

A way to solve this issue would be by nesting the function inside a pcall, like so

local status, response = pcall(function() printTest(t.example.example) end)

if (status == false) then
    printTest(response)
end

However, I am trying to make a function that is independent from having to be nested inside a pcall like that, or anything, that would let me call printTest(t.example.example) and catching the error. And so therefore I am looking for alternative solutions.

I want to implement this pcall check inside the function printTest(), but I am not sure if there's a way to do that.

I was thinking about something like printTest(protected_check(t.example.example)) or something that calls when printTest() has been tried to be called with something that would error, but I don't think it exists.



Solution 1:[1]

The 'test' in the pcall() could be: assert
Because assert can also used to have a default.
Look: https://www.lua.org/manual/5.4/manual.html#pdf-assert
Example

 local notexistent = false or nil
 local b, r = pcall(assert, notexistent, "Sorry - Thats not working")
 print(r)
-- Output: Sorry - Thats not working

As a default...
The assert() returning a string message (or number) if fail.
That message could be also a function as a string.
An assert() text message function could be converted to a function with: load()
And with load() you can give a function an own environment...

local notexistent = false or nil
local b, r = pcall(assert,
notexistent,
[[return function(msg) print(msg) end]])

if not b then
 r = load(r, 'assert_func_msg', 't', {print = io.write})()
 r('[' .. debug.getinfo(r).source .. ']\tSorry - This is not working\n')
end
-- Output: [assert_func_msg]    Sorry - This is not working

Look: https://www.lua.org/manual/5.4/manual.html#pdf-load

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1