This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
hbomb79's profile picture

[solved] loadstring error catching

Started by hbomb79, 14 November 2015 - 01:43 AM
hbomb79 #1
Posted 14 November 2015 - 02:43 AM
Lets say I use the following code:

local ok, err = loadstring([[
	_G.test = function()
		error("hey")
	end
]])
if not ok and err then
	print( err )
end
local ok, err = pcall( ok )
if not ok and err then print(err) end
test()

When I run this code, it will return an error: "string:2: hey". This makes debugging stupidly hard as the file is not specified. I would like to know if there a way to catch this error and append custom information to the front of it so I know what file this error originated from.

Because I have around 50 files being loaded through this method, I need the solution to be a runtime solution because the file won't be used immediately after loading. Other data will be loaded using this method before it maybe used.

Perhaps some sort of coroutine or wrapping of the function that will catch any errors that occur during runtime.

- Thanks, Harry.
Edited on 14 November 2015 - 02:00 AM
Anavrins #2
Posted 14 November 2015 - 02:56 AM
I'm pretty sure the second argument of loadstring does that.
loadstring([
] , "foobar")
Should error with "foobar:2 hey"
Edited on 14 November 2015 - 01:56 AM
hbomb79 #3
Posted 14 November 2015 - 03:00 AM
Oh, wow. Thanks a bunch, you are awesome.

Can't believe I didn't see that in the Lua pil :D/>.