From ee1e2caf3096b500733ea83009f44db4c3a083ea Mon Sep 17 00:00:00 2001 From: Linventif Date: Mon, 1 Jul 2024 05:05:49 +0000 Subject: [PATCH] add: error testing + report --- lua/gmod_integration/client/cl__color.lua | 5 -- lua/gmod_integration/shared/sh_errors.lua | 57 +++++++++++++---------- lua/gmod_integration/shared/sh_main.lua | 25 +++++++++- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/lua/gmod_integration/client/cl__color.lua b/lua/gmod_integration/client/cl__color.lua index a9e801a..bb25a43 100644 --- a/lua/gmod_integration/client/cl__color.lua +++ b/lua/gmod_integration/client/cl__color.lua @@ -18,11 +18,6 @@ local colorTbl = { ["font-secondary"] = Color(179, 179, 179) } -// con cmd how create a error -concommand.Add("gmi_error", function() - error("This is a test error") -end) - function gmInte.getColor(name) return colorTbl[name] end diff --git a/lua/gmod_integration/shared/sh_errors.lua b/lua/gmod_integration/shared/sh_errors.lua index c737c0c..5ba99cf 100644 --- a/lua/gmod_integration/shared/sh_errors.lua +++ b/lua/gmod_integration/shared/sh_errors.lua @@ -2,33 +2,42 @@ // Methods // -function gmInte.sendLuaErrorReport(err, realm, stack, name, id, uptime) - if (string.find(err, "Gmod Integration")) then return end +local cacheErrors = {} - if (SERVER && math.Round(RealTime()) == 0) then - return timer.Simple(1, function() - gmInte.sendLuaErrorReport(err, realm, stack, name, id, math.Round(RealTime())) - end) - end +function gmInte.sendLuaErrorReport(err, realm, stack, name, id) + cacheErrors[err] = { + ["time"] = CurTime(), + ["count"] = cacheErrors[err] && cacheErrors[err].count + 1 || 1, + } - if (CLIENT && (!IsValid(LocalPlayer()) || !gmInte.config.token)) then - return timer.Simple(1, function() - gmInte.sendLuaErrorReport(err, realm, stack, name, id, math.Round(RealTime())) - end) - end + if (!gmInte.config.id || !gmInte.config.token) then return end + if (CLIENT && !IsValid(LocalPlayer())) then return end - local endpoint = SERVER && "/servers/:serverID/errors" || "/clients/:steamID64/errors" - gmInte.http.post(endpoint, - { - ["error"] = err, - ["realm"] = realm, - ["stack"] = stack, - ["name"] = name, - ["id"] = id, - ["uptime"] = uptime || math.Round(RealTime()), - ["identifier"] = SERVER && gmInte.config.id || LocalPlayer():SteamID64() - } - ) + local count = cacheErrors[err].count + timer.Simple(0.5, function() + if (cacheErrors[err].count != count) then + if (cacheErrors[err].count == 100) then + else + return + end + else + cacheErrors[err] = nil + end + + local endpoint = SERVER && "/servers/:serverID/errors" || "/clients/:steamID64/servers/:serverID/errors" + gmInte.http.post(endpoint, + { + ["error"] = err, + ["realm"] = realm, + ["stack"] = stack, + ["name"] = name, + ["id"] = id, + ["count"] = count, + ["uptime"] = math.Round(RealTime()), + ["identifier"] = SERVER && gmInte.config.id || CLIENT && LocalPlayer():SteamID64() + } + ) + end) end // diff --git a/lua/gmod_integration/shared/sh_main.lua b/lua/gmod_integration/shared/sh_main.lua index f9c5f83..b81f1bc 100644 --- a/lua/gmod_integration/shared/sh_main.lua +++ b/lua/gmod_integration/shared/sh_main.lua @@ -37,4 +37,27 @@ function gmInte.isPrivateIP(ip) if (parts[1] == "172" && tonumber(parts[2]) >= 16 && tonumber(parts[2]) <= 31) then return true end if (parts[1] == "127") then return true end return false -end \ No newline at end of file +end + +concommand.Add("gmi_test_error", function( ply, cmd, args ) + print(ply, cmd) + PrintTable(args) + if (#args == 0) then + error("This is a test error") + else + if (args[1] == "loop") then + hook.Add("Think", "gmInte:TestError:Loop", function() + error("This is a test error") + end) + timer.Simple(5, function() + hook.Remove("Think", "gmInte:TestError:Loop") + end) + elseif (args[1] == "crash") then + while (true) do + error("This is a crash error") + end + else + error("This is a test error") + end + end +end) \ No newline at end of file