Page 1 of 1

More useful error messages?

Posted: Sun Nov 09, 2014 12:00 am
by Zilarrezko
Anyone know of a way to make error messages more useful?

Having to go back through old code and making stuff isometric is quite a pain. And it doesn't help when it throws an error on a function saying trying to index nil value when I'm passing in about 8 arguments or something. Maybe something that says which argument would be much more helpful.

But at least these error messages are better than Visual Studio's compiler error messages.... Those scare me :halloween: :o.

Or, maybe some tips as to create some more helpful feedback with error messages? That's fairly helpful too.

Re: More useful error messages?

Posted: Sun Nov 09, 2014 12:45 am
by davisdude
You could make a local function that checks. It's a bit of extra typing, and should probably be removed when you release a game, but a useful function I've found is this:

Code: Select all

local function CheckType( Name, Value, ... ) 
	local Acceptable = { ... }
	local Type = type( Value )
	local Passed = false
	for _, Value in ipairs( Acceptable ) do
		Passed = ( Type == Value ) or Passed
	end
	assert( Passed, 'Argument error: Expected argument \'' .. Name .. '\' to be type: ' .. table.concat( Acceptable, ', ' ) .. '. Got: ' .. Type )
end
Then use it like this:

Code: Select all

local function Distance( x1, y1, x2, y2 )
	CheckType( 'x1', x1, 'number' )
	CheckType( 'y1', y1, 'number' )
	CheckType( 'x2', x2, 'number' )
	CheckType( 'y2', y2, 'number' )
	return ( ( x1 - x2 ) ^ 2 + ( y1 - y2 ) ^ 2 ) ^ .5
end

Re: More useful error messages?

Posted: Sun Nov 09, 2014 1:27 am
by Zilarrezko
Interesting. Like assert but multiple type checks, I think I may use that in the future.

Still doesn't fix my original problem though. The trick is me trying to access an index to pass it into a functions argument. like function(table.table.value), but the middle table or value is nil... Not sure which, That's why I'm trying to find a better way to throw an error.

it'd be nice if It could do... function(table[index1][index2]). And if table[index1] is a nil value, then the error message could say "line 140: in function argument #1, table[index1] is a nil value." Then do it's traceback. Rather than "line 140: attempt to index nil value" :|

But again, cool function. Will use, much fun. Thanks :D

Re: More useful error messages?

Posted: Sun Nov 09, 2014 3:24 am
by davisdude
Well, passing it as a function and doing that function would be impossible, since it's nil you can't access it.
To do that, you would have to check it individually before you passed it. You would have to check each part individually.
i.e.:

Code: Select all

local function RecursiveCheck( Name, Table, Next, Index )
	Index = Index or {}
	if type( Table ) == 'table' then
		if Table[Next[1]] then
			Index[#Index + 1] = Next[1]
			local Reference = Next[1]
			table.remove( Next, 1 )
			RecursiveCheck( Name, Table[Reference], Next, Index )
		elseif #Next > 0 then
			Index[#Index + 1] = Next[1]
			error( 'Table error: The reference to ' .. table.concat( Index, ', ' ) .. ' does not exist.' )
		end
	end
end

local function Distance( Points )
	RecursiveCheck( 'Point1', Points, { 'Point1', 'x' } )
	RecursiveCheck( 'Point2', Points, { 'Point2', 'z' } )
	-- Distance Formula
end
	
Distance( { Point1 = { x = 1, y = 1, z = { 1, 2, 3 } }, Point2 = { x = 2, y = 1 } } )
I threw this together. Hopefully it's useful.
When you run this, the second line will run an error, since there isn't 'z' in the table.

Re: More useful error messages?

Posted: Sun Nov 09, 2014 1:05 pm
by Robin
The easiest thing would be to change:

Code: Select all

f(t1.t2.t3.v)
into:

Code: Select all

local t2 = t1.t2
local t3 = t2.t3
f(t3.v)