From ccad59fcf69bfd602be5d17be3434b3ddcf502c1 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 15 Dec 2022 11:24:08 +1100 Subject: [PATCH] fix(server/logger): validate response on failed http request One person is receiving a CURL error rather than a response from datadog, leading to a nil index. We'll also use the newly supported "warn" function. --- imports/logger/server.lua | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/imports/logger/server.lua b/imports/logger/server.lua index 7f4973d..a1f3845 100644 --- a/imports/logger/server.lua +++ b/imports/logger/server.lua @@ -3,16 +3,8 @@ local hostname = GetConvar('sv_projectName', 'fxserver') local buffer local bufferSize = 0 -local function badResponse(endpoint, response) - print(('unable to submit logs to %s\n%s'):format(endpoint, json.encode(response, { indent = true }))) -end - --- idk where to put this? - -local function split(str,pat) - local tbl = {} - str:gsub(pat, function(x) tbl[#tbl+1]=x end) - return tbl +local function badResponse(endpoint, status, response) + warn(('unable to submit logs to %s (status: %s)\n%s'):format(endpoint, status, json.encode(response, { indent = true }))) end local playerData = {} @@ -70,7 +62,10 @@ if service == 'datadog' then SetTimeout(500, function() PerformHttpRequest(endpoint, function(status, _, _, response) if status ~= 202 then - badResponse(endpoint, json.decode(response:sub(10)).errors[1]) + if type(response) == 'string' then + response = json.decode(response:sub(10)) or response + badResponse(endpoint, status, type(response) == 'table' and response.errors[1] or response) + end end end, 'POST', json.encode(buffer), headers) @@ -129,7 +124,7 @@ if service == 'loki' then local postBody = json.encode({streams = tempBuffer}) PerformHttpRequest(endpoint, function(status, _, _, _) if status ~= 204 then - badResponse(endpoint, ("Error Code: %s\n%s"):format(status, postBody)) + badResponse(endpoint, status, ("%s"):format(status, postBody)) end end, 'POST', postBody, { ['Content-Type'] = 'application/json',