Jump to content




[solved] loadstring error catching


  • You cannot reply to this topic
2 replies to this topic

#1 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

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 by Hbomb_79, 14 November 2015 - 03:00 AM.


#2 Anavrins

  • Members
  • 775 posts

Posted 14 November 2015 - 02:56 AM

I'm pretty sure the second argument of loadstring does that.
loadstring([[code]] , "foobar")
Should error with "foobar:2 hey"

Edited by Anavrins, 14 November 2015 - 02:56 AM.


#3 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users