From 823bcf9ddc3b3d5b1ed7070a0fbe6721fa17e3a7 Mon Sep 17 00:00:00 2001 From: Linventif Date: Fri, 29 Mar 2024 15:04:32 +0100 Subject: [PATCH] fix: better log --- lua/gmod_integration/shared/sh_http.lua | 29 ++++++++----------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/lua/gmod_integration/shared/sh_http.lua b/lua/gmod_integration/shared/sh_http.lua index df2041a..f1b4da9 100644 --- a/lua/gmod_integration/shared/sh_http.lua +++ b/lua/gmod_integration/shared/sh_http.lua @@ -42,7 +42,7 @@ function gmInte.http.requestAPI(params) local url = getAPIURL(params.endpoint) local method = params.method local success = params.success || function() end - local failed = params.failed || function() if (!gmInte.config.debug) then gmInte.log("HTTP Failed, if this error persists please contact support") end end + local failed = params.failed || function() end local version = gmInte.config.version local showableBody = showableBody(params.endpoint) local requestID = genRequestID() @@ -57,51 +57,40 @@ function gmInte.http.requestAPI(params) ["Authorization"] = "Bearer " .. token, ["Version"] = version } - local type = "application/json" - // Log gmInte.log("HTTP FQDN: " .. gmInte.config.apiFQDN, true) gmInte.log("HTTP Request ID: " .. requestID, true) gmInte.log("HTTP Request: " .. method .. " " .. url, true) gmInte.log("HTTP Body: " .. (showableBody && body || "HIDDEN"), true) - // Send HTTP({ ["url"] = url, ["method"] = method, ["headers"] = headers, ["body"] = body, - ["type"] = type, + ["type"] = "application/json", ["success"] = function(code, body, headers) - // Log gmInte.log("HTTP Request ID: " .. requestID, true) gmInte.log("HTTP Response: " .. code, true) - if (gmInte.config.debug) then gmInte.log("HTTP Body: " .. body, true) end + gmInte.log("HTTP Body: " .. body, true) + + if (code < 200 || code >= 300) then + gmInte.log("HTTP Failed: Invalid Status Code", true) + return failed(code, body) + end - // if not application/json return failed if (string.sub(headers["Content-Type"], 1, 16) != "application/json") then gmInte.log("HTTP Failed: Invalid Content-Type", true) - return failed({ ["error"] = "Invalid Content-Type" }, code, headers) + return failed(code, body) end - // Parse body body = util.JSONToTable(body || "{}") - // if not 2xx return failed - if (code < 200 || code >= 300) then - return failed(code, body, headers) - end - - // Return success return success(code, body) end, ["failed"] = function(error) - // Log gmInte.log("HTTP Request ID: " .. requestID, true) gmInte.log("HTTP Failed: " .. error, true) - - // Return failed - return failed({ ["error"] = error }) end }) end