Does anyone know how to throw a type error?

Like this,

if not is_string( data ) then error("bad argument #1 to 'WriteChunk' (string expected, got "..type(data)..")") end

Except I’m pretty sure there’s an already existing function that does this, but I can’t find it.

Use assert:

assert( type( x ) == "number", "bad type" )

Hmm not exactly what I’m looking for, but close. Maybe it is an internal c++ function. I’ll use that if I can’t find it.

C defined functions use LUA->CheckType to assert the proper argument ype. If it doesn’t match it throws that error from your first post. I don’t see anywhere where Lua defined functions throw type errors, most of them just return nil on error. But using assert like code_gs said is probably the best way to do it unless you’re making a module.

I’d suggest using error() over assert() so that you can set the error level to 2. Otherwise error messages will list your code at fault, rather than the callee.

Oh thanks. Setting the level was what I needed.

[editline]2nd May 2015[/editline]

function net.ReadChunk( ply, callback )

if CLIENT then 
	ply = NULL
	if type( ply ) ~= "Player" then
		error( "bad argument #1 to 'ReadChunk' (Player expected, got " .. type( ply ) .. ")", 2 )
	elseif not ply:IsValid() then
		error( "bad argument #1 to 'ReadChunk' (Tried to use a NULL entity!)", 2 )
if type( callback ) ~= "function" then
	error( "bad argument #2 to 'ReadChunk' (function expected, got " .. type( callback ) .. ")", 2 )


Hm for some reason it is showing my function in the stack anyway.

[ERROR] lua_run:1: bad argument #1 to 'ReadChunk' (Player expected, got Entity)
  1. error - [C]:-1
   2. ReadChunk - lua/includes/modules/net.lua:281
    3. unknown - lua_run:1

[editline]2nd May 2015[/editline]

Could there be a mod incorrectly overriding the error function?

You can check where a function is defined with debug.getinfo

lua_run PrintTable(debug.getinfo(error, "S"))

As for the issue itself I’m not sure.

Your function will always be part of the stack trace. It’s just the way life is.
What the stack level parameter does is change the primary error source displayed at the top.

Ah ok.