What does the error object from xpcall contain?

I couldn’t find documentation on this, and i’m having some trouble using it. I want to find the line in which the function called errors. debug.traceback() is broken and starts at the line in which it’s called and completely ignores the function that errored and the scope argument, so that doesn’t work.

2 Likes

no idea. but found the doc…

bool , Variant xpcall ( function f, function err, tuple args )
This function is similar to pcall, except that you can set a new error handler.

xpcall calls function f in protected mode, using err as the error handler, and passes a list of arguments. Any error inside f is not propagated; instead, xpcall catches the error, calls the err function with the original error object, and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In this case, xpcall also returns all results from the call, after this first result. In case of any error, xpcall returns false plus the result from err.

Lua Globals (roblox.com)

Well yes but i still don’t know the propetries / functions of the error object.

1 Like

From doing some light testing it looks like it’s string?
When you get a string it’s a traceback like what you see in a normal error.
I got it to be nil by doing this:

local function errorhandle(...) 
	local args = {...}
	print(type(args[1]),args)
	return "GotEm"
end

print(xpcall(error,errorhandle))
2 Likes

this post by @SuchTheLegendary explains it perfectly!

2 Likes

So the below script will work in most cases. PLEASE NOTE, there are going to be some edge cases where we can’t grab the line/reason or it gets messed up along the way. Also as noted in: debug | Roblox Creator Documentation

The format of the returned traceback is not defined and may change at any time; use only for debug diagnostics and error analytics. It’s recommended that you never parse the return value of this function for specific information, such as script names or line numbers.

local function b()
	return Instance.not.valid
end
local function a()
	b()
end
local function errorhandle(myError :string)
	local line, reason = myError:match(":(%d+):(.*)$")
	warn("\nLine:",line,"\nReasion:",reason)
end

print(xpcall(a,errorhandle))

To learn more about the string library: string | Roblox Creator Documentation
To lean more about patterns: String Patterns

1 Like

This gives me line: nil; reason: nil, calls to require really seem to break traceback things, the weirderst part is that the module results in no errors other than creating a studio warning "Layered Accessories JSON is invalid! - Studio" what can i try now?