Handling all errors myself

Hi,

I’d like to handle all errors using my own Lua code. The main reasons are logging and also to reduce the amount of console spam when errors are shown over and over.

I know that pcall() can be used to handle errors, but I’d like to do them throughout the entire project.

I tried overwriting Error(), ErrorNoHalt() and error() but this only allowed me to handle calls to these specific functions and not the errors from syntax errors with lua_run and so on.

Any ideas ? It would be unrealistic to execute all code via pcall().

Thanks.

Probably only possible through a binary module, i don’t know any way in Lua to get access to syntax errors.

Binary modules would be a problem for me as I also want to handle client side errors, without players having to download modules.

I suppose it could be a suggestion for Garry to add a way to hook these errors, if it can’t currently be done.

It’s not possible to handle all the errors yourself with pure Lua and very likely not even with a binary module since the error handling is built into GMod itself.

And it would destroy the purpose of it! It should give you the feeling that you HAVE to fix the error to stop the spam.

And this ^

The problem with the error spam is that error spam from an error which is not the cause of the problem can hide the error which is actually the cause, making it hard to debug.

Oh another unrelated question that I have: (I might as well post it in here.)
I am using input.IsKeyDown() for some keyboard input stuff. I want to use this function because it allows me to use any key from the keyboard. The issue is that it still handles keys when the player is in the console / menu / Steam friends. Is there any way to detect if the player is not “in” the game ?

That is why I always do small steps,(coding something, running, debugging, step 1…etc…) to see what I changed.

For error handling, I like to do things like:

[lua]
function DoSomething( arguments )

if ArgumentsAreBad( arguments ) then
error( “Something descriptive!”, 2 ) – -< the 2 is important!
return
end

DoNormalStuff()

end
[/lua]

This way, the error will be reported on the line you called DoSomething, and your custom error message will be printed instead.

Also, check out xpcall: (last paragraph)

You can take a regular block of shitty code like this:

[lua]
function foo( x )
return x + 5 + {}
end

print( foo( “ninnywits”, “hobgoblins” ) )
[/lua]

And wrap it with xpcall( function() …block… end, errorfunction ) like this:

[lua]
local erf = function( err )
print( debug.traceback() )
end

xpcall( function()

function foo( x )
return x + 5 + {}
end

print( foo( “ninnywits”, “hobgoblins” ) )

end, erf )
[/lua]

and handle errors yourself with the debug library in the second argument of xpcall.

For stopping spam though, I don’t know. Maybe we could ask garry for an option to surpress errors that arise over and over again in Think hooks?

Yea I only found out about the lowercase version of error() today when I was looking this up, seems very useful.

The problem with pcall() / xpcall() is that it would be impractical to wrap the entire of the code in it.

I see your point about it only being used in development, however I want to use this in released games so that I can track new bugs which appear with updates as well as find bugs which have been missed. By not relying on the players to report error messages (A lot of players tend to not notice the error messages, as they are not looking out for them.) the game stands a much better chance of coming close to that all important yet impossible “bug free” state.

I should probably point out to avoid confusion - This is for my own servers, not for released addons and the like.

Ahhh, I get you now. So you want to make something like microsoft’s automatic error reporting? I like that idea. But from what I can tell from the programming in lua book, error handling is done exclusively thorugh the C API, not lua. If everyone bothers him enough, maybe garry could add a hook that’s called when errors are thrown but before the stack unwinds, just like xpcall.

Edit:

In the meantime, I guess you could do some hacky shit like run a condump to the data directory (condump is blocked, but I guess there’s a way to hack around it) and have a parser that scours it for lines that are lua errors. This might piss garry off enough that he’ll add proper hooks for error logging.

I guess I could try something like that, I’ll have a look into it. Thanks.

Any ideas regarding the input.IsKeyDown() usage and detecting if the user is “in game” ?

Not sure if this helps but there is this command

lua_log_sv 1

It will log all errors to garrysmod/lua_errors_server.txt

Don’t make errors.

somtimes tracing errors can suck balls since they dont actually point to the errors

like errors that talk about string.find in one of your util functions or something

i believe the debug library is more than useful in this pursuit, ‘deco da man’ probably knows the most about the debug library so ask him.

That’s pretty useful thanks, I always wondered why there was a “lua_errors_server.txt” in the beta - I thought it was just something for the beta.

:frowning:

Please ? :frowning:

Yes this is one of those places that error( <Message>, 2 ) could come in handy, if we could handle those errors ourselves.

Not to mention error that appear out of nowhere from Gmod updates.

Yea there’s a lot of stuff regarding the debug library that even after reading the Lua documentation I still don’t really understand.