Removed useless code & improved error handler
This commit is contained in:
parent
fe4c648b07
commit
df41bf428b
@ -1,64 +1,63 @@
|
|||||||
|
local AuthorizationError = require(script.Parent.Errors.Authorization)
|
||||||
|
local CollectionError = require(script.Parent.Errors.Collection)
|
||||||
|
local EntryError = require(script.Parent.Errors.Entry)
|
||||||
|
|
||||||
local HttpService = game:GetService("HttpService")
|
local HttpService = game:GetService("HttpService")
|
||||||
|
|
||||||
function errorHandler(RBXSignal: {}, result: any)
|
function errorHandler(type: string, resultBody: any, resultObject: {})
|
||||||
local Error;
|
local Error;
|
||||||
if typeof(result) == typeof({}) and result["message"] then
|
if typeof(resultBody) == typeof({}) and resultBody["message"] then
|
||||||
Error = result["message"]
|
Error = resultBody["message"]
|
||||||
elseif typeof(result) == typeof("") then
|
elseif typeof(resultBody) == typeof("") then
|
||||||
Error = result
|
Error = resultBody
|
||||||
else
|
else
|
||||||
Error = "An Unexpected Error occoured."
|
Error = "An Unexpected Error occoured."
|
||||||
end
|
end
|
||||||
spawn(function()
|
|
||||||
RBXSignal:_Fire({
|
if type == "collection" then
|
||||||
["success"] = false,
|
if resultObject["StatusCode"] == 401 then
|
||||||
["errorMessage"] = Error
|
Error = AuthorizationError.InvalidAccessToken("InvalidAccessToken")
|
||||||
})
|
elseif resultObject["StatusCode"] == 404 then
|
||||||
end)
|
Error = CollectionError.CollectionNotFound("CollectionNotFound")
|
||||||
return {["onResult"] = RBXSignal}
|
elseif resultObject["StatusCode"] == 400 then
|
||||||
|
Error = CollectionError.CollectionAlreadyExists("CollectionAlreadyExists")
|
||||||
|
end
|
||||||
|
elseif type == "entry" or type == "entryId" then
|
||||||
|
if resultObject["StatusCode"] == 401 then
|
||||||
|
Error = AuthorizationError.InvalidAccessToken("InvalidAccessToken")
|
||||||
|
elseif resultObject["StatusCode"] == 404 then
|
||||||
|
Error = CollectionError.CollectionNotFound("CollectionNotFound")
|
||||||
|
elseif resultObject["StatusCode"] == 400 then
|
||||||
|
Error = EntryError.InvalidEntryData("InvalidEntryData")
|
||||||
|
elseif type == "entryId" and ((resultBody["modifiedEntries"] and resultBody["modifiedEntries"] == 0) or (resultBody["deletedEntries"] and resultBody["deletedEntries"] == 0)) then
|
||||||
|
Error = EntryError.EntryNotFound("EntryNotFound")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {["success"] = false, ["errorMessage"] = Error}
|
||||||
end
|
end
|
||||||
|
|
||||||
local utils = {}
|
local utils = {}
|
||||||
function utils._signal()
|
|
||||||
local RBXSignal = {}
|
|
||||||
RBXSignal._bindableEvent = Instance.new("BindableEvent")
|
|
||||||
function RBXSignal:_Fire(...)
|
|
||||||
RBXSignal._bindableEvent:Fire(...)
|
|
||||||
end
|
|
||||||
function RBXSignal:Connect(handler: ({success: boolean, result: {}}) -> ({success: boolean, result: {}}))
|
|
||||||
if typeof(self) ~= "table" then error("Please use : instead of .") end
|
|
||||||
if not (type(handler) == "function") then
|
|
||||||
error(("connect(%s)"):format(typeof(handler)), 2)
|
|
||||||
end
|
|
||||||
RBXSignal._bindableEvent.Event:Connect(function(...)
|
|
||||||
handler(...)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
return RBXSignal
|
|
||||||
end
|
|
||||||
function utils.handleResponse(result: any, error: boolean, signal: RBXScriptSignal?)
|
|
||||||
if error then return result end
|
|
||||||
spawn(function()
|
|
||||||
signal:_Fire(result)
|
|
||||||
end)
|
|
||||||
return {["onResult"] = signal}
|
|
||||||
end
|
|
||||||
|
|
||||||
function utils.makeHTTPRequest(method: string, url: string, body: {}, authorization: string):{["success"]: boolean, ["message"]: string}
|
function utils.makeHTTPRequest(type: string, method: string, url: string, body: {}, authorization: string):{["success"]: boolean, ["message"]: string}
|
||||||
local result;
|
local resultObj;
|
||||||
|
local resultBody;
|
||||||
local success = pcall(function()
|
local success = pcall(function()
|
||||||
if body then body = HttpService:JSONEncode(body) end
|
if body then body = HttpService:JSONEncode(body) end
|
||||||
if (method == "GET" or method == "HEAD") then
|
if (method == "GET" or method == "HEAD") then
|
||||||
result = HttpService:JSONDecode(HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}})["Body"])
|
resultObj = HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}})
|
||||||
|
resultBody = HttpService:JSONDecode(resultObj["Body"])
|
||||||
else
|
else
|
||||||
result = HttpService:JSONDecode(HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}, Body=body})["Body"])
|
resultObj = HttpService:RequestAsync({Method=method, Url=url, Headers={["Authorization"]=authorization,["Content-Type"]="application/json"}, Body=body})
|
||||||
|
resultBody = HttpService:JSONDecode(resultObj["Body"])
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
if success and result and result["success"] then
|
if success and resultBody and resultBody["success"] then
|
||||||
if result["warning"] then warn('[MarcSync HTTPRequest Handler] MarcSync HTTP Request returned warning for URL "'..url..'" with body: "'..HttpService:JSONEncode(body)..'": '..result["warning"]) end
|
if type == "entryId" and ((resultBody["modifiedEntries"] and resultBody["modifiedEntries"] == 0) or (resultBody["deletedEntries"] and resultBody["deletedEntries"] == 0)) then return errorHandler(type, resultBody, resultObj) end
|
||||||
return result, false, utils._signal()
|
if resultBody["warning"] then warn('[MarcSync HTTPRequest Handler] MarcSync HTTP Request returned warning for URL "'..url..'" with body: "'..HttpService:JSONEncode(body)..'": '..resultBody["warning"]) end
|
||||||
|
return resultBody
|
||||||
end
|
end
|
||||||
return errorHandler(utils._signal(), result), true
|
return errorHandler(type, resultBody, resultObj)
|
||||||
end
|
end
|
||||||
|
|
||||||
return utils
|
return utils
|
Loading…
x
Reference in New Issue
Block a user