[SNIPPET] Error Handling

Started by ysc3839, May 01, 2016, 06:50 AM

Previous topic - Next topic

ysc3839

Handles the error and print to console. You also can print to your own console.
function errorHandling(err)
{
local stackInfos = getstackinfos(2);

if (stackInfos)
{
local locals = "";

foreach( index, value in stackInfos.locals )
{
if( index != "this" )
locals = locals + "[" + index + "] " + value + "\n";
}

local callStacks = "";
local level = 2;
do {
callStacks += "*FUNCTION [" + stackInfos.func + "()] " + stackInfos.src + " line [" + stackInfos.line + "]\n";
level++;
} while ((stackInfos = getstackinfos(level)));

local errorMsg = "AN ERROR HAS OCCURRED [" + err + "]\n";
errorMsg += "\nCALLSTACK\n";
errorMsg += callStacks;
errorMsg += "\nLOCALS\n";
errorMsg += locals;

Console.Print(errorMsg);
}
}

seterrorhandler(errorHandling);
Code also can be found here: https://bitbucket.org/snippets/ysc3839/p64jG

Test code:
function Script::ScriptLoad()
{
local t = null;
Console.Print(t.t);
}

Output:
AN ERROR HAS OCCURRED [the index 't' does not exist]

CALLSTACK
*FUNCTION [ScriptLoad()] Main Script line [37]

LOCALS
[this] (class : 0x0EEA1820)
[t] (null : 0x00000000)

DizzasTeR

Ah that's better, now no need to go at debugfile the whole freaking time, thanks!

kingofpopYT


ysc3839

Quote from: coalman on May 03, 2016, 09:37 AMwhere do we have to add?
On the top of your script. Or use dofile.

Luis_Labarca

hey bro I have a problem I add Console.Print ("test") or Print ("test"); in Script::ScriptLoad() and I get nothing on console
My server RP
IP: 51.222.28.159:8194

Thijn

It should be in your log file, not in your console.

Luis_Labarca

Quote from: Thijn on Jul 24, 2016, 08:39 PMIt should be in your log file, not in your console.
Ah do not know that bro I set thanks ;)
My server RP
IP: 51.222.28.159:8194