How can i "hide" a function from error leveling?

How can I “hide” a (wrapper) function from error leveling?

Here is an example of what i mean:

local function f(a, b)
    error("Some error", 2)
end
local function wrapper_func(...)
    return f(...)
end
wrapper_func("foo", "bar") --errors at line 5 instead of here
f("foo", "bar") --errors here

I want the error to appear in the global scope, on line 7, but instead it errors in the wrapper_func.

I dont want to use getfenv because it introduces a lot of problems, what if i want to runt the function sometimes with the wrapper function and sometimes not wrapped.

The original solution is pretty simple. Second argument of lua error global is level, respectively error position.

Considering your example, the following are results we get from using different level values:

local function func() error("Some error", 4);end
local function wrapper() return func(); end
wrapper();

--[[
	error("Some error", 1) == error("Some error")
		OUTPUT >> line 1
	-----------------------------------------------
	error("Some error", 2)
		OUTPUT >> line 2
	-----------------------------------------------
	error("Some error", 3)
		OUTPUT >> line 3
	-----------------------------------------------
	error("Some error", 4) or more
		OUTPUT >> no position
]]

Level one is the origin, level two is the next step in the shell, and any value higher thanlevel of function that was called and consequentially raised an error removes the position display. Same happens if level is set to 0.

What is a relatively simple yet unfortunately not so practical way to display errors as expected?

local function func(wrapped)
	local errLevel = wrapped and 3 or 2;
	error("Some error", errLevel);
end
local function wrapper()
	return func(true);
end
wrapper(); 		-- Error at line 8.
func(false);	-- Error at line 9.

EDIT I don’t see any serious problems with using getfenv(), although the above method should work just fine for simple cases.